| 1 | 8 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | EJS::Template::IO - Mix-in module to normalize input/output parameters for EJS::Template | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package EJS::Template::IO; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use IO::Scalar; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 Methods | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head2 input | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Normalizes input. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | $self->input('filepath.ejs'); | ||||||||||||||||||||||||||||
| 23 | $self->input(\$source_text); | ||||||||||||||||||||||||||||
| 24 | $self->input($input_handle); | ||||||||||||||||||||||||||||
| 25 | $self->input(\*STDIN); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | It returns a list in the form C<($input, $should_close)>, where C<$input> is | ||||||||||||||||||||||||||||
| 28 | the normalized input handle and C<$should_close> indicates the file handle has | ||||||||||||||||||||||||||||
| 29 | been opened and your code is responsible for closing it. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Alternatively, a callback can be given as the second argument, which will be invoked | ||||||||||||||||||||||||||||
| 32 | with its argument set to the normalized C<$input>. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | $self->input('filepath.ejs', sub { | ||||||||||||||||||||||||||||
| 35 | my ($input) = @_; | ||||||||||||||||||||||||||||
| 36 | while (<$input>) { | ||||||||||||||||||||||||||||
| 37 | ... | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | }); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | If C<$input> is a file handle that has been opened by this C<input()> method, then | ||||||||||||||||||||||||||||
| 42 | it will be closed automatically after the callback returns. | ||||||||||||||||||||||||||||
| 43 | Even if C<die()> is invoked within the callback, the file handle will be closed if | ||||||||||||||||||||||||||||
| 44 | necessary, and then this C<input()> method will forward C<die($@)>. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 229 | ▶ | sub input { | ||||||||||||||||||||||||||
|
Subroutine
input: called CC=10 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 229 | my ($self, $input, $callback) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 229 | my $in; | |||||||||||||||||||||||||||
| 52 | 229 | my $should_close = 0; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 229 | ▶ | if (!defined $input && defined $self->{in}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $input and defined $self->{"in"}
MC/DC0%0/2
not defined $input and defined $self->{"in"}
not defined $input
defined $self->{"in"}
Truth table33%1/3
not(A) and B
A= not defined $inputB= defined $self->{"in"}
| |||||||||||||||||||||||||||||
| 55 | 0 | $input = $self->{in}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 229 | ▶ | if (defined $input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 229 | ▶ | if (openhandle($input)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | 0 | $in = $input; | |||||||||||||||||||||||||||
| 61 | } elsif (ref $input) { | ||||||||||||||||||||||||||||
| 62 | 221 | $in = IO::Scalar->new($input); | |||||||||||||||||||||||||||
| 63 | 221 | $should_close = 1; | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 8 | ▶ | open $in, $input or die "$!: $input"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 8 | $should_close = 1; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 0 | $in = \*STDIN; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 229 | ▶ | if ($callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 146 | eval { | |||||||||||||||||||||||||||
| 74 | 146 | local $self->{in} = $in; | |||||||||||||||||||||||||||
| 75 | 146 | $callback->($in); | |||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 146 | my $e = $@; | |||||||||||||||||||||||||||
| 79 | 146 | ▶ | close $in if $should_close; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 146 | ▶ | die $e if $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 83 | return ($in, $should_close); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 output | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Normalizes output. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | $self->output('filepath.out'); | ||||||||||||||||||||||||||||
| 91 | $self->output(\$result_text); | ||||||||||||||||||||||||||||
| 92 | $self->output($output_handle); | ||||||||||||||||||||||||||||
| 93 | $self->output(\*STDOUT); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | It returns a list in the form C<($output, $should_close)>, where C<$output> is | ||||||||||||||||||||||||||||
| 96 | the normalized output handle and C<$should_close> indicates the file handle has | ||||||||||||||||||||||||||||
| 97 | been opened and your code is responsible for closing it. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Alternatively, a callback can be given as the second argument, which will be invoked | ||||||||||||||||||||||||||||
| 100 | with its argument set to the normalized C<$output>. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | $self->output('filepath.out', sub { | ||||||||||||||||||||||||||||
| 103 | my ($output) = @_; | ||||||||||||||||||||||||||||
| 104 | while (<$output>) { | ||||||||||||||||||||||||||||
| 105 | ... | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | }); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | If C<$output> is a file handle that has been opened by this C<output()> method, then | ||||||||||||||||||||||||||||
| 110 | it will be closed automatically after the callback returns. | ||||||||||||||||||||||||||||
| 111 | Even if C<die()> is invoked within the callback, the file handle will be closed if | ||||||||||||||||||||||||||||
| 112 | necessary, and then this C<output()> method will forward C<die($@)>. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 146 | ▶ | sub output { | ||||||||||||||||||||||||||
|
Subroutine
output: called CC=10 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 146 | my ($self, $output, $callback) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 146 | my $out; | |||||||||||||||||||||||||||
| 120 | 146 | my $should_close = 0; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 146 | ▶ | if (!defined $output && defined $self->{out}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $output and defined $self->{"out"}
MC/DC50%1/2
not defined $output and defined $self->{"out"}
not defined $output
defined $self->{"out"}
Truth table66%2/3
not(A) and B
A= not defined $outputB= defined $self->{"out"}
| |||||||||||||||||||||||||||||
| 123 | 3 | $output = $self->{out}; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 146 | ▶ | if (defined $output) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 146 | ▶ | if (openhandle $output) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 128 | 11 | $out = $output; | |||||||||||||||||||||||||||
| 129 | } elsif (ref $output) { | ||||||||||||||||||||||||||||
| 130 | 135 | $$output = ''; | |||||||||||||||||||||||||||
| 131 | 135 | $out = IO::Scalar->new($output); | |||||||||||||||||||||||||||
| 132 | 135 | $should_close = 1; | |||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | open($out, '>', $output) or die "$!: $output"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | $should_close = 1; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | 0 | $out = \*STDOUT; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 146 | ▶ | if ($callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 146 | eval { | |||||||||||||||||||||||||||
| 143 | 146 | local $self->{out} = $out; | |||||||||||||||||||||||||||
| 144 | 146 | $callback->($out); | |||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 146 | my $e = $@; | |||||||||||||||||||||||||||
| 148 | 146 | ▶ | close $out if $should_close; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 146 | ▶ | die $e if $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||
| 151 | 0 | return ($out, $should_close); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 print | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Prints text to the current output target. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | It can be invoked only within an execution context where the output file handle is open. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | $self->output('filepath.out', sub { | ||||||||||||||||||||||||||||
| 162 | $self->print(...); | ||||||||||||||||||||||||||||
| 163 | }); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | ▶ | sub print { | ||||||||||||||||||||||||||
|
Subroutine
print: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 169 | 4 | my $out = $self->{out}; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 4 | ▶ | unless ($out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | die "print() can be invoked only within an execution context where the output file handle is open"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 4 | print $out $_ for @_; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =over 4 | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =item * L<EJS::Template> | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =back | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 8 | 1; | |||||||||||||||||||||||||||