File Coverage

blib/lib/Test/Inline.pm
Criterion Covered Total %
statement 198 240 82.5
branch 63 120 52.5
condition 6 13 46.1
subroutine 42 46 91.3
pod 18 18 100.0
total 327 437 74.8


line stmt bran cond sub pod time code
1             package Test::Inline;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Test::Inline - Embed your tests in your code, next to what is being tested
8              
9             =head1 DESCRIPTION
10              
11             Embedding tests allows tests to be placed near the code being tested.
12              
13             This is a nice supplement to the traditional .t files.
14              
15             =head2 How does it work?
16              
17             C lets you write small fragments of general or
18             function-specific testing code, and insert it anywhere you want in your
19             modules, inside a specific tagged L segment, like the
20             following.
21              
22             =begin testing
23            
24             # This code assumes we have a cpuinfo file
25             ok( -f /proc/cpuinfo, 'Host has a standard /proc/cpuinfo file' );
26            
27             =end testing
28            
29             =begin testing label
30            
31             # Test generation of the
32             is( My::HTML->label('foo'), '', '->label(simple) works' );
33             is( My::HTML->label('bar', 'foo'), '', '->label(for) works' );
34            
35             =end testing
36              
37             You can add as many, or as few, of these chunks of tests as you wish.
38             The key condition when writing them is that they should be logically
39             independant of each other. Each chunk of testing code should not die
40             or crash if it is run before or after another chunk.
41              
42             Using L or another test compiler, you can then transform
43             these chunks in a test script, or an entire tree of modules into a
44             complete set of standard L-based test scripts.
45              
46             These test scripts can then be executed as normal.
47              
48             =head2 What is Test::Inline good for?
49              
50             C is incredibly useful for doing ad-hoc unit testing.
51              
52             In any large groups of modules, you can add testing code here, there and
53             everywhere, anywhere you want. The next time the test compiler is run, a
54             new test script will just appear.
55              
56             This also makes it great for testing assumptions you normally wouldn't
57             bother to write run-time code to test. It ensures that your assumptions
58             about the way Perl does some operation, or about the state of the host,
59             are confirmed at install-time.
60              
61             If your assumption is ever wrong, it gets picked up at install-time and
62             based on the test failures, you can correct your assumption.
63              
64             It's also extremely useful for systematically testing self-contained code.
65              
66             That is, any code which can be independantly tested without the need for
67             external systems such as databases, and that has no side-effects on external
68             systems.
69              
70             All of this code, written by multiple people, can then have one single set
71             of test files generated. You can check all the bits and pieces of a large
72             API, or anything you like, in fine detail.
73              
74             Test::Inline also introduces the concept of unit-tested documentation.
75              
76             Not only can your code be tested, but if you have a FAQ or some other
77             pure documentation module, you can validate that the documentation is
78             correct for the version of the module installed.
79              
80             If the module ever changes to break the documentation, you can catch it
81             and correct the documentation.
82              
83             =head2 What is Test::Inline bad for?
84              
85             C is B a complete testing solution, and there are several
86             types of testing you probably DON'T want to use it for.
87              
88             =over
89              
90             =item *
91              
92             Static testing across the entire codebase
93              
94             =item *
95              
96             Functional testing
97              
98             =item *
99              
100             Tests with side-effects such as those that might change a testing database
101              
102             =back
103              
104             =head2 Getting Started
105              
106             Because Test::Inline creates test scripts with file names that B
107             start with a number (for ordering purposes), the first step is to create
108             your normal test scripts using file names in the CPAN style of
109             F<01_compile.t>, F<02_main.t>, F<03_foobar.t>, and so on.
110              
111             You can then add your testing fragments wherever you like throughout
112             your code, and use the F script to generate the test scripts
113             for the inline tests. By default the test scripts will be named after
114             the packages/classes that the test fragments are found in.
115              
116             Tests for Class::Name will end up in the file C.
117              
118             These test files sit quite happily alongside your number test scripts.
119              
120             When you run the test suite as you normally would, the inline scripts
121             will be run after the numbered tests.
122              
123             =head1 METHODS
124              
125             =cut
126              
127 12     12   293006 use 5.006;
  12         48  
  12         445  
128 12     12   67 use strict;
  12         23  
  12         398  
129 12     12   13560 use IO::Handle ();
  12         103126  
  12         306  
130 12     12   101 use List::Util 1.19 ();
  12         425  
  12         249  
131 12     12   65 use File::Spec 0.80 ();
  12         309  
  12         249  
132 12     12   11733 use File::Slurp 9999.04 ();
  12         192425  
  12         334  
133 12     12   10308 use Params::Util 0.21 ();
  12         86954  
  12         848  
134 12     12   12396 use Algorithm::Dependency 1.02 ();
  12         43977  
  12         264  
135 12     12   87 use Algorithm::Dependency::Source ();
  12         25  
  12         170  
136 12     12   6840 use Test::Inline::Util ();
  12         28  
  12         202  
137 12     12   7732 use Test::Inline::Section ();
  12         37  
  12         277  
138 12     12   7912 use Test::Inline::Script ();
  12         33  
  12         277  
139 12     12   6909 use Test::Inline::Content ();
  12         30  
  12         337  
140 12     12   8514 use Test::Inline::Content::Legacy ();
  12         27  
  12         210  
141 12     12   6043 use Test::Inline::Content::Default ();
  12         31  
  12         228  
142 12     12   6112 use Test::Inline::Content::Simple ();
  12         28  
  12         700  
143 12     12   8335 use Test::Inline::Extract ();
  12         31  
  12         239  
144 12     12   7148 use Test::Inline::IO::File ();
  12         35  
  12         431  
145              
146 12     12   71 use vars qw{$VERSION @ISA};
  12         22  
  12         783  
147             BEGIN {
148 12     12   35 $VERSION = '2.213';
149 12         31822 @ISA = 'Algorithm::Dependency::Source';
150             }
151              
152              
153              
154              
155              
156             #####################################################################
157             # Constructor and Accessors
158              
159             =pod
160              
161             =head2 new
162              
163             my $Tests = Test::Inline->new(
164             verbose => 1,
165             readonly => 1,
166             output => 'auto',
167             manifest => 'auto/manifest',
168             );
169              
170             The C constructor creates a new test generation framework. Once the
171             constructor has been used to create the generator, the C method
172             can be used to specify classes, or class heirachies, to generate tests for.
173              
174             B - The C option causes the generator to write state and
175             debugging information to STDOUT as it runs.
176              
177             B - The C option, if provided, will cause a manifest
178             file to be created and written to disk. The manifest file contains a list
179             of all the test files generated, but listed in the prefered order they
180             should be processed to best satisfy the class-level dependency of the
181             tests.
182              
183             B - The C value controls how strictly the
184             test script will watch the number of tests that have been executed.
185              
186             When set to false, the script does no count checking other than the
187             standard total count for scripts (where all section counts are known)
188              
189             When set to C<1> (the default), C does smart count checking,
190             doing section-by-section checking for known-count sections B when
191             the total for the entire script is not known.
192              
193             When set to C<2> or higher, C does full count checking,
194             doing section-by-section checking for every section with a known number
195             of tests.
196              
197             B - The C option should be provided as a CODE
198             reference, which will be passed as arguments the C object,
199             and a single L object, and should return a string
200             containing the contents of the resulting test file. This will be written
201             to the C.
202              
203             B - The C option provides the location of the directory
204             where the tests will be written to. It should both already exist, and be
205             writable. If using a custom C, the value of C should
206             refer to the location B that the files will be
207             written to.
208              
209             B - The C option, if provided, indicates that any
210             generated test files should be created (or set when updated) with
211             read-only permissions, to prevent accidentally adding to or editing the
212             test scripts directly (instead of via the classes).
213              
214             This option is currently disabled by default, by may be enabled by default
215             in a future release, so if you do NOT want your tests being created as
216             read-only, you should explicitly set this option to false.
217              
218             B - The C option, if provided, supplies an
219             alternative C from which source modules are retrieved.
220              
221             B - The C option, if provided, supplies an
222             alternative C to which the resulting test scripts are written.
223              
224             Returns a new C object on success.
225              
226             Returns C if there is a problem with one of the options.
227              
228             =cut
229              
230             # For now, the various Handlers are hard-coded
231             sub new {
232 17     17 1 12099 my $class = Params::Util::_CLASS(shift);
233 17         203 my %params = @_;
234 17 50       67 unless ( $class ) {
235 0         0 die '->new is a static method';
236             }
237              
238             # Create the object
239 17         149 my $self = bless {
240             # Return errors via exceptions?
241             exception => !! $params{exception},
242              
243             # Extensibility provided through the use of Handler classes
244             InputHandler => $params{InputHandler},
245             ExtractHandler => $params{ExtractHandler},
246             ContentHandler => $params{ContentHandler},
247             OutputHandler => $params{OutputHandler},
248              
249             # Store the ::TestFile objects
250             Classes => {},
251             }, $class;
252              
253             # Run in verbose mode?
254 17         142 $self->{verbose} = !! $params{verbose};
255              
256             # Generate tests with read-only permissions?
257 17         67 $self->{readonly} = !! $params{readonly};
258              
259             # Generate a manifest file?
260 17 100       65 $self->{manifest} = $params{manifest} if $params{manifest};
261              
262             # Do count checking?
263 17 100       93 $self->{check_count} = exists $params{check_count}
    100          
    100          
264             ? $params{check_count}
265             ? $params{check_count} >= 2
266             ? 2 # Paranoid count checking
267             : 1 # Smart count checking
268             : 0 # No count checking
269             : 1; # Smart count checking (default)
270              
271             # Support the legacy file_content param
272 17 50       68 if ( $params{file_content} ) {
273 0 0       0 Params::Util::_CODE($params{file_content}) or return undef;
274 0 0       0 $self->{ContentHandler} = Test::Inline::Content::Legacy->new(
275             $params{file_content}
276             ) or return undef;
277             }
278              
279             # Set the default Handlers
280 17   50     110 $self->{ExtractHandler} ||= 'Test::Inline::Extract';
281 17   33     233 $self->{ContentHandler} ||= Test::Inline::Content::Default->new;
282 17   33     328 $self->{InputHandler} ||= Test::Inline::IO::File->new( File::Spec->curdir );
283 17   33     156 $self->{OutputHandler} ||= Test::Inline::IO::File->new(
284             path => File::Spec->curdir,
285             readonly => $self->{readonly},
286             );
287              
288             # Where to write test file to, within the context of the OutputHandler
289 17 100       71 $self->{output} = defined $params{output} ? $params{output} : '';
290              
291 17         61 $self;
292             }
293              
294             =pod
295              
296             =head2 exception
297              
298             The C method returns a flag which indicates whether error will
299             be returned via exceptions.
300              
301             =cut
302              
303             sub exception {
304 0     0 1 0 $_[0]->{exception};
305             }
306              
307             =pod
308              
309             =head2 InputHandler
310              
311             The C method returns the file handler object that will be
312             used to find and load the source code.
313              
314             =cut
315              
316             sub InputHandler {
317 35     35 1 196 $_[0]->{InputHandler};
318             }
319              
320             =pod
321              
322             =head2 ExtractHandler
323              
324             The C accessor returns the object that will be used
325             to extract the test sections from the source code.
326              
327             =cut
328              
329             sub ExtractHandler {
330 20     20 1 205 $_[0]->{ExtractHandler};
331             }
332              
333             =pod
334              
335             =head2 ContentHandler
336              
337             The C accessor return the script content generation handler.
338              
339             =cut
340              
341             sub ContentHandler {
342 15     15 1 76 $_[0]->{ContentHandler};
343             }
344              
345             =pod
346              
347             =head2 OutputHandler
348              
349             The C accessor returns the file handler object that the
350             generated test scripts will be written to.
351              
352             =cut
353              
354             sub OutputHandler {
355 18     18 1 83 $_[0]->{OutputHandler};
356             }
357              
358              
359              
360              
361              
362             #####################################################################
363             # Test::Inline Methods
364              
365             =pod
366              
367             =head2 add $file, $directory, \$source, $Handle
368              
369             The C method is a parameter-sensitive method for adding something
370             to the build schedule.
371              
372             It takes as argument a file path, a directory path, a reference to a SCALAR
373             containing perl code, or an L (or subclass) object. It will
374             retrieve code from the parameter as appropriate, parse it, and create zero
375             or more L objects representing the test scripts that
376             will be generated for that source code.
377              
378             Returns the number of test scripts added, which could be zero, or C
379             on error.
380              
381             =cut
382              
383             sub add {
384 13     13 1 6627 my $self = shift;
385 13 50       56 my $source = $self->_source(shift) or return undef;
386 13 100       61 if ( ref $source ) {
387             # Add a chunk of source code
388 12         70 return $self->_add_source($source);
389             } else {
390             # Add a whole directory
391 1         8 return $self->_add_directory($source);
392             }
393             }
394              
395             =pod
396              
397             =head2 add_class
398              
399             $Tests->add_class( 'Foo::Bar' );
400             $Tests->add_class( 'Foo::Bar', recursive => 1 );
401              
402             The C method adds a class to the list of those to have their tests
403             generated. Optionally, the C option can be provided to add not just
404             the class you provide, but all classes below it as well.
405              
406             Returns the number of classes found with inline tests, and added, including
407             C<0> if no classes with tests are found. Returns C if an error occurs
408             while adding the class or it's children.
409              
410             =cut
411              
412             sub add_class {
413 0     0 1 0 my $self = shift;
414 0 0       0 my $name = shift or return undef;
415 0         0 my %options = @_;
416              
417             # Determine the files to add
418 0         0 $self->_verbose("Checking $name\n");
419 0 0       0 my $files = $options{recursive}
420             ? $self->InputHandler->find( $name )
421             : $self->InputHandler->file( $name );
422 0 0       0 return $files unless $files; # 0 or undef
423              
424             # Add the files
425 0         0 my $added = 0;
426 0         0 foreach my $file ( @$files ) {
427 0         0 my $rv = $self->add( $file );
428 0 0       0 return undef unless defined $rv;
429 0         0 $added += $rv;
430             }
431              
432             # Clear the caches
433 0         0 delete $self->{schedule};
434 0         0 delete $self->{filenames};
435              
436 0         0 $added;
437             }
438              
439             =pod
440              
441             =head2 add_all
442              
443             The C method will search the C for all *.pm files,
444             and add them to the generation set.
445              
446             Returns the total number of test scripts added, which may be zero, or
447             C on error.
448              
449             =cut
450              
451             sub add_all {
452 1     1 1 506 my $self = shift;
453 1         3 my $rv = eval {
454 1         4 $self->_add_directory('.');
455             };
456 1 50       4 return $self->_error($@) if $@;
457 1         4 return $rv;
458             }
459              
460             # Recursively add an entire directory of files
461             sub _add_directory {
462 2     2   4 my $self = shift;
463              
464             # Find all module files in the directory
465 2 50       7 my $files = $self->InputHandler->find(shift) or return undef;
466              
467             # Add each file
468 2         7 my $added = 0;
469 2         7 foreach my $file ( @$files ) {
470 7 50       27 my $source = $self->InputHandler->read($file) or return undef;
471 7         26 my $rv = $self->_add_source($source);
472 7 50       21 return undef unless defined $rv;
473 7         20 $added += $rv;
474             }
475              
476 2         10 $added;
477             }
478              
479             # Actually add the source code
480             sub _add_source {
481 20     20   664 my $self = shift;
482 20 50       119 my $source = Params::Util::_SCALAR(shift) or return undef;
483              
484             # Extract the elements from the source code
485 20 50       76 my $Extract = $self->ExtractHandler->new( $source )
486             or return $self->_error("Failed to create ExtractHandler");
487 20 100       86 my $elements = $Extract->elements or return 0;
488              
489             # Parse the elements into sections
490 17 100       188 my $Sections = Test::Inline::Section->parse( $elements )
491             or return $self->_error("Failed to parse sections: $Test::Inline::Section::errstr");
492              
493             # Split up the Sections by class
494 16         57 my %classes = ();
495 16         39 foreach my $Section ( @$Sections ) {
496             # All sections MUST have a package
497 49 50       151 my $context = $Section->context
498             or return $self->_error("Section does not have a package context");
499 49   100     202 $classes{$context} ||= [];
500 49         56 push @{$classes{$context}}, $Section;
  49         118  
501             }
502              
503             # Convert the collection of Sections into class-specific test file objects
504 16         41 my $added = 0;
505 16         46 my $Classes = $self->{Classes};
506 16         50 foreach my $_class ( keys %classes ) {
507             # We can't safely spread tests for the same class across
508             # different files. Error if we spot a duplicate.
509 20 50       59 if ( $Classes->{$_class} ) {
510 0         0 return $self->_error("Caught duplicate test class");
511             }
512              
513             # Create a new ::TestFile object for the collection of Sections
514 20 50       159 my $File = Test::Inline::Script->new(
515             $_class,
516             $classes{$_class},
517             $self->{check_count}
518             ) or return $self->_error("Failed to create a new TestFile for '$_class'");
519 20         115 $self->_verbose("Adding $File to schedule\n");
520 20         42 $Classes->{$_class} = $File;
521 20         50 $added++;
522             }
523              
524 16         601 $added++;
525             }
526              
527             =pod
528              
529             =head2 classes
530              
531             The C method returns a list of the names of all the classes that
532             have been added to the C object, or the null list C<()> if
533             nothing has been added.
534              
535             =cut
536              
537             sub classes {
538 5     5 1 11 my $self = shift;
539 5         9 sort keys %{$self->{Classes}};
  5         47  
540             }
541              
542             =pod
543              
544             =head2 class
545              
546             For a given class name, fetches the L object for that
547             class, if it has been added to the C object. Returns C
548             if the class has not been added to the C object.
549              
550             =cut
551              
552 23     23 1 867 sub class { $_[0]->{Classes}->{$_[1]} }
553              
554             =pod
555              
556             =head2 filenames
557              
558             For all of the classes added, the C method generates a map of the
559             filenames that the test files for the various classes should be written to.
560              
561             Returns a reference to a hash with the classes as keys, and filenames as
562             values.
563              
564             Returns C<0> if there are no files to write.
565              
566             Returns C on error.
567              
568             =cut
569              
570             sub filenames {
571 35     35 1 43 my $self = shift;
572 35 100       145 return $self->{filenames} if $self->{filenames};
573              
574             # Create an Algorithm::Dependency for the classes
575 6 50       87 my $Algorithm = Algorithm::Dependency::Ordered->new(
576             source => $self,
577             ignore_orphans => 1,
578             ) or return undef;
579              
580             # Get the build schedule
581 6         260 $self->_verbose("Checking dependencies\n");
582 6 50       40 unless ( $Algorithm->source->items ) {
583 0         0 return 0;
584             }
585 6 50       44 my $schedule = $Algorithm->schedule_all or return undef;
586              
587             # Merge the test position counter with the class base names
588 6         102 my %filenames = ();
589 6         30 for ( my $i = 0; $i <= $#$schedule; $i++ ) {
590 14         28 my $class = $schedule->[$i];
591 14         55 $filenames{$class} = $self->{Classes}->{$class}->filename;
592             }
593              
594 6         23 $self->{schedule} = [ map { $filenames{$_} } @$schedule ];
  14         36  
595 6         186 $self->{filenames} = \%filenames;
596             }
597              
598             =pod
599              
600             =head2 schedule
601              
602             While the C method generates a map of the files for the
603             various classes, the C returns the list of file names in the
604             order in which they should actually be executed.
605              
606             Returns a reference to an array containing the file names as strings.
607              
608             Returns C<0> if there are no files to write.
609              
610             Returns C on error.
611              
612             =cut
613              
614             sub schedule {
615 8     8 1 14 my $self = shift;
616 8 100       161 return $self->{schedule} if $self->{schedule};
617              
618             # Generate the file names and schedule
619 1 50       5 $self->filenames or return undef;
620              
621 1         6 $self->{schedule};
622             }
623              
624             =pod
625              
626             =head2 manifest
627              
628             The C generates the contents of the manifest file, if it is both
629             wanted and needed.
630              
631             Returns the contents of the manifest file as a normal string, false if it
632             is either not wanted or needed, or C on error.
633              
634             =cut
635              
636             sub manifest {
637 8     8 1 18 my $self = shift;
638              
639             # Do we need to create a file?
640 8 50       29 my $schedule = $self->schedule or return undef;
641 8 50       33 return '' unless $self->{manifest};
642 8 50       26 return '' unless @$schedule;
643              
644             # Each manifest entry should be listed by it's path relative to
645             # the location of the manifest file.
646 8         189 my $manifest_dir = (File::Spec->splitpath($self->{manifest}))[1];
647 8         85 my $relative_path = Test::Inline::Util->relative(
648             $manifest_dir => $self->{output},
649             );
650 8 50       26 return undef unless defined $relative_path;
651              
652             # Generate and merge the manifest entries
653 8         25 my @manifest = @$schedule;
654 8 100       37 if ( length $relative_path ) {
655 7         17 @manifest = map { File::Spec->catfile( $relative_path, $_ ) } @manifest;
  25         226  
656             }
657 8         20 join '', map { "$_\n" } @manifest;
  26         89  
658             }
659              
660             =pod
661              
662             =head2 save
663              
664             $Tests->save;
665              
666             The C method generates the test files for all classes, and saves them
667             to the C directory.
668              
669             Returns the number of test files generated. Returns C on error.
670              
671             =cut
672              
673             sub save {
674 4     4 1 5592 my $self = shift;
675              
676             # Get the file names to save to
677 4         21 my $filenames = $self->filenames;
678 4 50       18 return $filenames unless $filenames; # undef or 0
679              
680             # Write the manifest if needed
681 4         19 my $manifest = $self->manifest;
682 4 50       17 return undef unless defined $manifest;
683 4 50       12 if ( $manifest ) {
684 4 50       18 if ( $self->OutputHandler->write( $self->{manifest}, $manifest ) ) {
685 4         23 $self->_verbose( "Wrote manifest file '$self->{manifest}'\n" );
686             } else {
687 0         0 $self->_verbose( "Failed to write manifest file '$self->{manifest}'\n" );
688 0         0 return undef;
689             }
690             }
691              
692             # Write the files
693 4         8 my $written = 0;
694 4         28 foreach my $class ( sort keys %$filenames ) {
695 14 50       38 $self->_save( $class ) or return undef;
696 14         29 $written++;
697             }
698              
699 4         34 $written;
700             }
701              
702             sub _file {
703 29     29   115 my $self = shift;
704 29 50       71 my $filenames = $self->filenames or return undef;
705 29         106 $filenames->{$_[0]};
706             }
707              
708             sub _save {
709 14     14   18 my $self = shift;
710 14 50       34 my $class = shift or return undef;
711 14 50       31 my $filename = $self->_file($class) or return undef;
712 14         54 local $| = 1;
713              
714             # Write the file
715 14 50       40 my $content = $self->_content($class) or return undef;
716 14         31 $self->_verbose("Saving...");
717 14 50       45 if ( $self->{output} ) {
718 14         156 $filename = File::Spec->catfile( $self->{output}, $filename );
719             }
720 14 50       47 unless ( $self->OutputHandler->write( $filename, $content ) ) {
721 0         0 $self->_verbose("Failed\n");
722 0         0 return undef;
723             }
724 14         51 $self->_verbose("Done\n");
725              
726 14         62 1;
727             }
728              
729             sub _content {
730 15     15   18 my $self = shift;
731 15 50       39 my $class = shift or return undef;
732 15 50       31 my $filename = $self->_file($class) or return undef;
733 15 50       42 my $Script = $self->class($class) or return undef;
734              
735             # Get the file content
736 15         61 $self->_verbose("Generating $filename for $class...");
737 15         36 my $content = $self->ContentHandler->process( $self, $Script );
738 15 50       39 $self->_verbose("Failed\n") unless defined $content;
739              
740 15         46 $content; # content or undef
741             }
742              
743              
744              
745              
746              
747             #####################################################################
748             # Implement the Algorithm::Dependency::Source Interface
749              
750 0     0 1 0 sub load { 1 }
751 42     42 1 719 sub item { $_[0]->{Classes}->{$_[1]} }
752             sub items {
753 12     12 1 77 my $classes = shift->{Classes};
754 12         53 map { $classes->{$_} } sort keys %$classes;
  28         75  
755             }
756              
757              
758              
759              
760              
761             #####################################################################
762             # Support Methods
763              
764             # Get the source code from a variety of places
765             sub _source {
766 13     13   21 my $self = shift;
767 13 50       44 return undef unless defined $_[0];
768 13 50       40 unless ( ref $_[0] ) {
769 13 100       48 if ( $self->InputHandler->exists_file($_[0]) ) {
    50          
770             # File path
771 12         49 return $self->InputHandler->read(shift);
772             } elsif ( $self->InputHandler->exists_dir($_[0]) ) {
773             # Directory path
774 1         4 return shift; # Handled seperately
775             }
776 0         0 return undef;
777             }
778 0 0       0 if ( Params::Util::_SCALAR($_[0]) ) {
779             # Reference to SCALAR containing code
780 0         0 return shift;
781             }
782 0 0       0 if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') ) {
783 0         0 my $fh = shift;
784 0         0 my $old = $fh->input_record_separator(undef);
785 0         0 my $code = $fh->getline;
786 0         0 $fh->input_record_separator($old);
787 0         0 return \$code;
788             }
789              
790             # Unknown
791 0         0 undef;
792             }
793              
794             # Print a message if we are in verbose mode
795             sub _verbose {
796 73     73   102 my $self = shift;
797 73 50       215 return 1 unless $self->{verbose};
798 0           print @_;
799             }
800              
801             # Warn and return
802             sub _error {
803 0     0     my $self = shift;
804 0 0         if ( $self->exception ) {
805 0           Carp::croak("Error: $_[0]");
806             }
807 0           $self->_verbose(map { "Error: $_" } @_);
  0            
808 0           undef;
809             }
810              
811             1;
812              
813             =pod
814              
815             =head1 BUGS
816              
817             The "Extended =begin" syntax used for non-trivial sections is not formalised
818             as part of the POD spec yet, although it is on the track to being included.
819              
820             While simple '=begin testing' sections are fine and will pass POD testing,
821             extended begin sections may cause POD errors.
822              
823             =head1 TO DO
824              
825             - Add support for C sections
826              
827             - Add support for C<=for> sections
828              
829             =head1 SUPPORT
830              
831             Bugs should always be submitted via the CPAN bug tracker
832              
833             L
834              
835             Professional support, assistance, or customisations for large scale
836             uses of C are available from L.
837              
838             For other issues, contact the maintainer.
839              
840             =head1 AUTHOR
841              
842             Adam Kennedy Eadamk@cpan.orgE
843              
844             =head1 ACKNOWLEDGEMENTS
845              
846             Thank you to Phase N (L) for permitting
847             the open sourcing and release of this distribution.
848              
849             =head1 COPYRIGHT
850              
851             Copyright 2004 - 2013 Adam Kennedy.
852              
853             This program is free software; you can redistribute
854             it and/or modify it under the same terms as Perl itself.
855              
856             The full text of the license can be found in the
857             LICENSE file included with this module.
858              
859             =cut