File Coverage

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


line stmt bran cond sub pod time code
1             package App::Pod::Example;
2              
3 4     4   74831 use strict;
  4         24  
  4         107  
4 4     4   23 use warnings;
  4         8  
  4         106  
5              
6 4     4   1877 use Class::Utils qw(set_params);
  4         52241  
  4         78  
7 4     4   274 use English qw(-no_match_vars);
  4         7  
  4         23  
8 4     4   1321 use Error::Pure qw(err);
  4         9  
  4         2000  
9 4     4   6273 use File::Temp qw(tempfile);
  4         77986  
  4         273  
10 4     4   7736 use Getopt::Std;
  4         209  
  4         249  
11 4     4   1744 use IO::Barf qw(barf);
  4         2447  
  4         62  
12 4     4   2063 use Pod::Example qw(get);
  4         110967  
  4         94  
13 4     4   275 use Readonly;
  4         9  
  4         3226  
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.20;
22              
23             # Constructor.
24             sub new {
25 18     18 1 56595 my ($class, @params) = @_;
26              
27             # Create object.
28 18         81 my $self = bless {}, $class;
29              
30             # Process params.
31 18         204 set_params($self, @params);
32              
33             # Object.
34 16         279 return $self;
35             }
36              
37             # Run.
38             sub run {
39 16     16 1 89 my $self = shift;
40              
41             # Process arguments.
42 16         121 $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 16 100 66     214 if (! getopts('d:ehn:prs:', $self->{'_opts'}) || @ARGV < 1
      66        
52             || $self->{'_opts'}->{'h'}) {
53              
54 1         123 print STDERR "Usage: $0 [-d flag] [-e] [-h] [-n number] ".
55             "[-p] [-r]\n\t[-s section] [--version] ".
56             "pod_file_or_module [argument ..]\n\n";
57 1         15 print STDERR "\t-d flag\t\tTurn debug (0/1) (default is 1).\n";
58 1         13 print STDERR "\t-e\t\tEnumerate lines. Only for print mode.\n";
59 1         14 print STDERR "\t-h\t\tHelp.\n";
60 1         13 print STDERR "\t-n number\tNumber of example (default is ".
61             "nothing).\n";
62 1         11 print STDERR "\t-p\t\tPrint example.\n";
63 1         13 print STDERR "\t-r\t\tRun example.\n";
64 1         12 print STDERR "\t-s section\tUse section (default EXAMPLE).\n";
65 1         25 print STDERR "\t--version\tPrint version.\n";
66 1         5 return 1;
67             }
68 15         1669 $self->{'_pod_file_or_module'} = shift @ARGV;
69 15         42 $self->{'_args'} = \@ARGV;
70 15         47 $self->{'_debug'} = $self->{'_opts'}->{'d'};
71 15         33 $self->{'_enumerate'} = $self->{'_opts'}->{'e'};
72 15         35 $self->{'_number'} = $self->{'_opts'}->{'n'};
73 15         32 $self->{'_print'} = $self->{'_opts'}->{'p'};
74 15         44 $self->{'_run'} = $self->{'_opts'}->{'r'};
75 15         31 $self->{'_section'} = $self->{'_opts'}->{'s'};
76              
77             # No action.
78 15 100 100     127 if (! $self->{'_print'} && ! $self->{'_run'}) {
79 1         5 err 'Cannot process any action (-p or -r options).';
80             }
81              
82             # Get example code.
83 14         129 my $code = get($self->{'_pod_file_or_module'}, $self->{'_section'}, $self->{'_number'});
84              
85             # No code.
86 14 100       97668 if (! defined $code) {
87 1         36 print "No code.\n";
88 1         13 return 0;
89             }
90              
91             # Print.
92 13 100       53 if ($self->{'_print'}) {
93 3 100       10 if ($self->{'_debug'}) {
94 1         12 _debug('Example source');
95             }
96 3 100       18 if ($self->{'_enumerate'}) {
97 1         8 my @lines = split "\n", $code;
98 1         3 my $count = 1;
99 1         3 foreach my $line (@lines) {
100 5         74 print $count.': '.$line."\n";
101 5         18 $count++;
102             }
103             } else {
104 2         50 print $code."\n";
105             }
106             }
107              
108             # Run.
109 13 100       36 if ($self->{'_run'}) {
110 10 100       28 if ($self->{'_debug'}) {
111 9         27 _debug('Example output');
112             }
113 10         42 my (undef, $tempfile) = tempfile();
114 10         3870 barf($tempfile, $code);
115 10         118108 my $args = $EMPTY_STR;
116 10 100       18 if (@{$self->{'_args'}}) {
  10         53  
117 1         10 $args = '"'.(join '" "', @{$self->{'_args'}}).'"';
  1         7  
118             }
119 10         117535 system "$EXECUTABLE_NAME $tempfile $args";
120 10         1430 unlink $tempfile;
121             }
122              
123 13         507 return 0;
124             }
125              
126             sub _debug {
127 10     10   26 my $text = shift;
128 10         455 print $HASH, $DASH x 79, "\n";
129 10         151 print $HASH, $SPACE, $text."\n";
130 10         144 print $HASH, $DASH x 79, "\n";
131 10         34 return;
132             }
133              
134             1;
135              
136             __END__