File Coverage

blib/lib/App/Pod/Example.pm
Criterion Covered Total %
statement 89 89 100.0
branch 18 18 100.0
condition 9 9 100.0
subroutine 13 13 100.0
pod 2 2 100.0
total 131 131 100.0


line stmt bran cond sub pod time code
1             package App::Pod::Example;
2              
3 4     4   143077 use strict;
  4         7  
  4         128  
4 4     4   27 use warnings;
  4         4  
  4         178  
5              
6 4     4   2115 use Class::Utils qw(set_params);
  4         56716  
  4         79  
7 4     4   303 use English qw(-no_match_vars);
  4         12  
  4         22  
8 4     4   1504 use Error::Pure qw(err);
  4         9  
  4         281  
9 4     4   3984 use File::Temp qw(tempfile);
  4         173327  
  4         341  
10 4     4   2067 use Getopt::Std;
  4         9200  
  4         310  
11 4     4   2074 use IO::Barf qw(barf);
  4         2911  
  4         86  
12 4     4   2404 use Pod::Example qw(get);
  4         142470  
  4         108  
13 4     4   321 use Readonly;
  4         7  
  4         4953  
14              
15             # Constants.
16             Readonly::Scalar my $DASH => q{-};
17             Readonly::Scalar my $EMPTY_STR => q{};
18             Readonly::Scalar my $HASH => q{#};
19             Readonly::Scalar my $SPACE => q{ };
20              
21             our $VERSION = 0.22;
22              
23             # Constructor.
24             sub new {
25 20     20 1 824061 my ($class, @params) = @_;
26              
27             # Create object.
28 20         82 my $self = bless {}, $class;
29              
30             # Process params.
31 20         156 set_params($self, @params);
32              
33             # Object.
34 18         203 return $self;
35             }
36              
37             # Run.
38             sub run {
39 18     18 1 29 my $self = shift;
40              
41             # Process arguments.
42 18         160 $self->{'_opts'} = {
43             'd' => 0,
44             'h' => 0,
45             'e' => 0,
46             'n' => undef,
47             'p' => 0,
48             'r' => 0,
49             's' => 'EXAMPLE',
50             };
51 18 100 100     170 if (! getopts('d:ehn:prs:', $self->{'_opts'})
      100        
52             || $self->{'_opts'}->{'h'}
53             || @ARGV < 1) {
54              
55 3         494 print STDERR "Usage: $0 [-d flag] [-e] [-h] [-n number] ".
56             "[-p] [-r]\n\t[-s section] [--version] ".
57             "pod_file_or_module [argument ..]\n\n";
58 3         140 print STDERR "\t-d flag\t\tTurn debug (0/1) (default is 1).\n";
59 3         66 print STDERR "\t-e\t\tEnumerate lines. Only for print mode.\n";
60 3         50 print STDERR "\t-h\t\tHelp.\n";
61 3         48 print STDERR "\t-n number\tNumber of example (default is ".
62             "nothing).\n";
63 3         59 print STDERR "\t-p\t\tPrint example.\n";
64 3         50 print STDERR "\t-r\t\tRun example.\n";
65 3         47 print STDERR "\t-s section\tUse section (default EXAMPLE).\n";
66 3         48 print STDERR "\t--version\tPrint version.\n";
67 3         24 return 1;
68             }
69 15         1360 $self->{'_pod_file_or_module'} = shift @ARGV;
70 15         59 $self->{'_args'} = \@ARGV;
71 15         45 $self->{'_debug'} = $self->{'_opts'}->{'d'};
72 15         57 $self->{'_enumerate'} = $self->{'_opts'}->{'e'};
73 15         52 $self->{'_number'} = $self->{'_opts'}->{'n'};
74 15         48 $self->{'_print'} = $self->{'_opts'}->{'p'};
75 15         68 $self->{'_run'} = $self->{'_opts'}->{'r'};
76 15         34 $self->{'_section'} = $self->{'_opts'}->{'s'};
77              
78             # No action.
79 15 100 100     141 if (! $self->{'_print'} && ! $self->{'_run'}) {
80 1         26 err 'Cannot process any action (-p or -r options).';
81             }
82              
83             # Get example code.
84 14         13170 my $code = get($self->{'_pod_file_or_module'}, $self->{'_section'}, $self->{'_number'});
85              
86             # No code.
87 14 100       120739 if (! defined $code) {
88 1         35 print "No code.\n";
89 1         4 return 0;
90             }
91              
92             # Print.
93 13 100       58 if ($self->{'_print'}) {
94 3 100       9 if ($self->{'_debug'}) {
95 1         6 _debug('Example source');
96             }
97 3 100       7 if ($self->{'_enumerate'}) {
98 1         4 my @lines = split "\n", $code;
99 1         1 my $count = 1;
100 1         2 foreach my $line (@lines) {
101 5         16552 print $count.': '.$line."\n";
102 5         25 $count++;
103             }
104             } else {
105 2         45 print $code."\n";
106             }
107             }
108              
109             # Run.
110 13 100       2810 if ($self->{'_run'}) {
111 10 100       53 if ($self->{'_debug'}) {
112 9         38 _debug('Example output');
113             }
114 10         56 my (undef, $tempfile) = tempfile();
115 10         7300 barf($tempfile, $code);
116 10         2455 my $args = $EMPTY_STR;
117 10 100       42 if (@{$self->{'_args'}}) {
  10         52  
118 1         7 $args = '"'.(join '" "', @{$self->{'_args'}}).'"';
  1         4  
119             }
120 10         5097283 system "$EXECUTABLE_NAME $tempfile $args";
121 10         1973 unlink $tempfile;
122             }
123              
124 13         439 return 0;
125             }
126              
127             sub _debug {
128 10     10   54 my $text = shift;
129 10         619 print $HASH, $DASH x 79, "\n";
130 10         315 print $HASH, $SPACE, $text."\n";
131 10         156 print $HASH, $DASH x 79, "\n";
132 10         33 return;
133             }
134              
135             1;
136              
137             __END__