| 1 | package goto::file; | ||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 6 | our $VERSION = '0.005'; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 7 | ▶ | use Filter::Util::Call qw/filter_add filter_read/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | our %HANDLES; | ||||||||||||||
| 10 | |||||||||||||||
| 11 | my $ID = 1; | ||||||||||||||
| 12 | 7 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||
| 13 | 5 | my $class = shift; | |||||||||||||
| 14 | 5 | my ($in) = @_; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 5 | ▶ | return unless $in; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | my ($pkg, $file, $line) = caller(0); | |||||||||||||
| 19 | 1 | my ($fh, @lines); | |||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | ▶ | if (ref($in) eq 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | 1 | my $safe = $file; | |||||||||||||
| 23 | 4 | $safe =~ s/"/\\"/; | |||||||||||||
| 24 | |||||||||||||||
| 25 | 4 | push @lines => "#line " . (__LINE__ + 1) . ' "' . __FILE__ . '"'; | |||||||||||||
| 26 | 4 | push @lines => ( | |||||||||||||
| 27 | "package main;", | ||||||||||||||
| 28 | "#line 1 \"lines from $safe line $line\"", | ||||||||||||||
| 29 | @$in, | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | } | ||||||||||||||
| 32 | else { | ||||||||||||||
| 33 | 4 | push @lines => "#line " . (__LINE__ + 1) . ' "' . __FILE__ . '"'; | |||||||||||||
| 34 | 4 | push @lines => "package main;"; | |||||||||||||
| 35 | 4 | push @lines => "\$@ = '';"; | |||||||||||||
| 36 | |||||||||||||||
| 37 | 4 | my $id = $ID++; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 4 | ▶ | open($fh, '<', $in) or die "Cold not open file '$in': $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | 4 | $HANDLES{$id} = $fh; | |||||||||||||
| 42 | 4 | my $safe = $in; | |||||||||||||
| 43 | 5 | $safe =~ s/"/\\"/; | |||||||||||||
| 44 | 86 | push @lines => "#line " . (__LINE__ + 2) . ' "' . __FILE__ . '"'; | |||||||||||||
| 45 | 86 | push @lines => ( | |||||||||||||
| 46 | 10 | '{ local ($!, $?, $^E, $@); close(DATA); *DATA = $' . __PACKAGE__ . '::HANDLES{' . $id . '} }', | |||||||||||||
| 47 | qq{#line 1 "$safe"}, | ||||||||||||||
| 48 | ); | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 5 | Filter::Util::Call::filter_add( | |||||||||||||
| 52 | bless {fh => $fh, lines => \@lines, file => $in, caller => [$pkg, $file, $line]}, | ||||||||||||||
| 53 | $class | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 86 | ▶ | sub filter { | ||||||||||||
|
Subroutine
filter: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 58 | 5 | my $self = shift; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 23 | ▶ | unless ($self->{init}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 5 | $self->{init} = 1; | |||||||||||||
| 62 | 86 | ▶ | while (1) { filter_read() or last } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 86 | $_ = ''; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 86 | my $lines = $self->{lines}; | |||||||||||||
| 67 | 30 | my $fh = $self->{fh}; | |||||||||||||
| 68 | |||||||||||||||
| 69 | 30 | my $line; | |||||||||||||
| 70 | 55 | ▶ | if (@$lines) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 71 | 55 | chomp($line = shift @$lines); | |||||||||||||
| 72 | 86 | $line .= "\n"; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | elsif($fh) { | ||||||||||||||
| 75 | # We do this to prevent ', <$fh> at line #' being appended to | ||||||||||||||
| 76 | # exceptions and warnings. | ||||||||||||||
| 77 | 83 | local $.; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 83 | $line = <$fh>; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 3 | ▶ | if (defined $line) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 6 | $_ .= $line; | |||||||||||||
| 84 | 6 | return 1; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 6 | return 0; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 1; | ||||||||||||||
| 91 | |||||||||||||||
| 92 | __END__ | ||||||||||||||