File Coverage

blib/lib/App/Perl/Module/Examples.pm
Criterion Covered Total %
statement 65 65 100.0
branch 10 10 100.0
condition 8 8 100.0
subroutine 11 11 100.0
pod 2 2 100.0
total 96 96 100.0


line stmt bran cond sub pod time code
1             package App::Perl::Module::Examples;
2              
3 4     4   159115 use strict;
  4         9  
  4         139  
4 4     4   22 use warnings;
  4         14  
  4         248  
5              
6 4     4   2154 use Class::Utils qw(set_params);
  4         52802  
  4         75  
7 4     4   2507 use File::Find::Rule;
  4         39931  
  4         43  
8 4     4   2333 use File::Spec::Functions qw(abs2rel catdir catfile);
  4         3504  
  4         341  
9 4     4   5106 use Getopt::Std;
  4         12137  
  4         293  
10 4     4   1923 use IO::Barf qw(barf);
  4         2971  
  4         64  
11 4     4   2226 use Pod::Example 0.17 qw(get sections);
  4         132129  
  4         88  
12              
13             our $VERSION = 0.04;
14              
15             # Constructor.
16             sub new {
17 8     8 1 628807 my ($class, @params) = @_;
18              
19             # Create object.
20 8         23 my $self = bless {}, $class;
21              
22             # Process parameters.
23 8         39 set_params($self, @params);
24              
25             # Object.
26 7         55 return $self;
27             }
28              
29             # Run.
30             sub run {
31 6     6 1 9 my $self = shift;
32              
33             # Process arguments.
34 6         26 $self->{'_opts'} = {
35             'd' => 0,
36             'h' => 0,
37             };
38 6 100 100     24 if (! getopts('dh', $self->{'_opts'})
      100        
39             || $self->{'_opts'}->{'h'}
40             || @ARGV > 1) {
41              
42 3         282 print STDERR "Usage: $0 [-d] [-h] [--version] [working_dir]\n";
43 3         49 print STDERR "\t-d\t\tDebug mode.\n";
44 3         23 print STDERR "\t-h\t\tPrint help.\n";
45 3         22 print STDERR "\t--version\tPrint version.\n";
46 3         21 print STDERR "\t[working_dir]\tWorking directory (default is actual).\n";
47 3         9 return 1;
48             }
49 3   100     116 my $working_dir = $ARGV[0] || '.';
50              
51             # Find all perl module files in working directory.
52 3         27 my $rule = File::Find::Rule->new;
53 3         58 my @pm = $rule->or(
54             $rule->new->directory->name('t')->prune->discard,
55             $rule->new->directory->name('inc')->prune->discard,
56             $rule->new->directory->name('blib')->prune->discard,
57             $rule->new,
58             )->name('*.pm')->in($working_dir);
59              
60             # Print Perl modules in debug mode.
61 3 100       5083 if ($self->{'_opts'}->{'d'}) {
62 1         35 print "Found Perl modules:\n";
63 1         3 foreach my $pm (@pm) {
64 1         10 print '- '.abs2rel($pm, $working_dir)."\n";
65             }
66             }
67              
68             # For each example save example.
69 3         94 my $num = 1;
70 3         6 foreach my $perl_module_file (@pm) {
71              
72             # Get all example sections.
73 4         14 my @examples = sections($perl_module_file);
74              
75             # For each section.
76 4         24368 foreach my $example_sec (@examples) {
77 5         13 my ($section, $number_of_example) = _section_and_number($example_sec);
78              
79             # Create example content.
80 5         12 my ($example_data, $example_file) = get($perl_module_file, $section,
81             $number_of_example);
82 5 100       36686 if (! defined $example_file) {
83 1         4 $example_file = sprintf 'ex%d.pl', $num;
84             }
85 5         10 $example_data = "#!/usr/bin/env perl\n\n".
86             $example_data;
87 5         22 my $examples_dir = catdir($working_dir, 'examples');
88 5         18 my $example_path = catfile($examples_dir, $example_file);
89              
90             # Examples directory.
91 5 100       136 if (! -r $examples_dir) {
92 2         147 mkdir $examples_dir;
93             }
94              
95             # Save example.
96 5         26 barf($example_path, $example_data);
97 5         1010 chmod 0755, $example_path;
98              
99 5         22 $num++;
100             }
101             }
102            
103 3         68 return 0;
104             }
105              
106             # Get section name and number of example.
107             sub _section_and_number {
108 5     5   8 my $example_sec = shift;
109              
110 5 100       32 if ($example_sec =~ m/^(.+?)(\d+)$/ms) {
111 2         10 return ($1, $2);
112             }
113              
114 3         9 return ($example_sec, undef);
115             }
116              
117             1;
118              
119             __END__