File Coverage

blib/lib/App/Perl/Module/Examples.pm
Criterion Covered Total %
statement 36 57 63.1
branch 1 8 12.5
condition 1 3 33.3
subroutine 10 10 100.0
pod 2 2 100.0
total 50 80 62.5


line stmt bran cond sub pod time code
1             package App::Perl::Module::Examples;
2              
3 4     4   161881 use strict;
  4         7  
  4         128  
4 4     4   17 use warnings;
  4         17  
  4         207  
5              
6 4     4   1917 use Class::Utils qw(set_params);
  4         55552  
  4         76  
7 4     4   2283 use File::Find::Rule;
  4         36411  
  4         32  
8 4     4   1975 use File::Spec::Functions qw(catfile);
  4         2990  
  4         266  
9 4     4   1922 use Getopt::Std;
  4         11886  
  4         287  
10 4     4   1640 use IO::Barf qw(barf);
  4         2725  
  4         62  
11 4     4   2198 use Pod::Example qw(get sections);
  4         146124  
  4         164  
12              
13             our $VERSION = 0.03;
14              
15             # Constructor.
16             sub new {
17 3     3 1 727619 my ($class, @params) = @_;
18              
19             # Create object.
20 3         8 my $self = bless {}, $class;
21              
22             # Process parameters.
23 3         17 set_params($self, @params);
24              
25             # Object.
26 2         19 return $self;
27             }
28              
29             # Run.
30             sub run {
31 1     1 1 3 my $self = shift;
32              
33             # Process arguments.
34 1         11 $self->{'_opts'} = {
35             'd' => 0,
36             'h' => 0,
37             };
38 1 50 33     28 if (! getopts('dh', $self->{'_opts'})
39             || $self->{'_opts'}->{'h'}) {
40              
41 1         134 print STDERR "Usage: $0 [-d] [-h] [--version]\n";
42 1         33 print STDERR "\t-d\t\tDebug mode.\n";
43 1         17 print STDERR "\t-h\t\tPrint help.\n";
44 1         14 print STDERR "\t--version\tPrint version.\n";
45 1         4 return 1;
46             }
47              
48             # Find all perl module files in actual directory.
49 0           my $rule = File::Find::Rule->new;
50 0           my @pm = $rule->or(
51             $rule->new->directory->name('t')->prune->discard,
52             $rule->new->directory->name('inc')->prune->discard,
53             $rule->new->directory->name('blib')->prune->discard,
54             $rule->new,
55             )->name('*.pm')->in('.');
56              
57             # Dump perl modules in debug mode.
58 0 0         if ($self->{'_opts'}->{'d'}) {
59 0           require Dumpvalue;
60 0           my $dump = Dumpvalue->new;
61 0           $dump->dumpValues(\@pm);
62             }
63              
64             # For each example save example.
65 0           my $num = 1;
66 0           foreach my $perl_module_file (@pm) {
67              
68             # Get all example sections.
69 0           my @examples = sections($perl_module_file);
70              
71             # For each section.
72 0           foreach my $example_sec (@examples) {
73              
74             # Create example content.
75 0           my ($example_data, $example_file) = get($perl_module_file, $example_sec);
76 0 0         if (! defined $example_file) {
77 0           $example_file = sprintf 'ex%d.pl', $num;
78             }
79 0           $example_data = "#!/usr/bin/env perl\n\n".
80             $example_data;
81 0           my $example_path = catfile('examples', $example_file);
82              
83             # Examples directory.
84 0 0         if (! -r 'examples') {
85 0           mkdir 'examples';
86             }
87              
88             # Save example.
89 0           barf($example_path, $example_data);
90 0           chmod 0755, $example_path;
91              
92 0           $num++;
93             }
94             }
95            
96 0           return 0;
97             }
98              
99             1;
100              
101             __END__