| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | package # hide from pause | ||||||||||||||||||||||||||||
| 3 | File::Replace::SingleHandle; | ||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings::register; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw/blessed weaken/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # For AUTHOR, COPYRIGHT, AND LICENSE see the bottom of this file | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.18'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ## no critic (RequireFinalReturn, RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | require Tie::Handle::Base; | |||||||||||||||||||||||||||
| 18 | 2 | our @ISA = qw/ Tie::Handle::Base /; ## no critic (ProhibitExplicitISA) | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 37 | ▶ | sub TIEHANDLE { | ||||||||||||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 22 | 37 | ▶ | @_==3 or croak __PACKAGE__."->TIEHANDLE: bad number of args"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 36 | my ($class,$repl,$mode) = @_; | |||||||||||||||||||||||||||
| 24 | 36 | ▶ | croak "$class->TIEHANDLE: argument must be a File::Replace object" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($repl) and $repl->isa("File::Replace")
MC/DC100%2/2
builtin::blessed($repl) and $repl->isa("File::Replace")
builtin::blessed($repl)
$repl->isa("File::Replace")
Truth table100%3/3
A and B
A= builtin::blessed($repl)B= $repl->isa("File::Replace")
| |||||||||||||||||||||||||||||
| 25 | unless blessed($repl) && $repl->isa('File::Replace'); | ||||||||||||||||||||||||||||
| 26 | 34 | my ($innerhandle,$other); | |||||||||||||||||||||||||||
| 27 | 34 | ▶ | if ($mode eq 'in') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 28 | 15 | $innerhandle = $repl->in_fh; | |||||||||||||||||||||||||||
| 29 | 15 | $other = $repl->out_fh; } | |||||||||||||||||||||||||||
| 30 | elsif ($mode eq 'out') { | ||||||||||||||||||||||||||||
| 31 | 15 | $innerhandle = $repl->out_fh; | |||||||||||||||||||||||||||
| 32 | 15 | $other = $repl->in_fh; } | |||||||||||||||||||||||||||
| 33 | elsif ($mode eq 'onlyout') { | ||||||||||||||||||||||||||||
| 34 | 3 | $innerhandle = $repl->out_fh; } | |||||||||||||||||||||||||||
| 35 | 1 | else { croak "bad mode" } | |||||||||||||||||||||||||||
| 36 | 33 | my $self = $class->SUPER::TIEHANDLE($innerhandle); | |||||||||||||||||||||||||||
| 37 | 33 | $self->{repl} = $repl; | |||||||||||||||||||||||||||
| 38 | 33 | $self->{other} = $other; | |||||||||||||||||||||||||||
| 39 | 33 | weaken( $self->{other} ); | |||||||||||||||||||||||||||
| 40 | 33 | return $self; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 9 | ▶ | sub replace { return shift->{repl} } | ||||||||||||||||||||||||||
|
Subroutine
replace: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | sub in_fh { return shift->{repl}->in_fh } | ||||||||||||||||||||||||||
|
Subroutine
in_fh: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub out_fh { return shift->{repl}->out_fh } | ||||||||||||||||||||||||||
|
Subroutine
out_fh: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | sub OPEN { croak "Can't reopen a ".ref($_[0])." handle" } | ||||||||||||||||||||||||||
|
Subroutine
OPEN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 21 | ▶ | sub CLOSE { | ||||||||||||||||||||||||||
|
Subroutine
CLOSE: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 50 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 21 | ▶ | if ( defined($self->{other}) && defined(fileno($self->{other})) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"other"} and defined fileno $self->{"other"}
MC/DC100%2/2
defined $self->{"other"} and defined fileno $self->{"other"}
defined $self->{"other"}
defined fileno $self->{"other"}
Truth table100%3/3
A and B
A= defined $self->{"other"}B= defined fileno $self->{"other"}
| |||||||||||||||||||||||||||||
| 52 | # the other file is still open, so just close this one | ||||||||||||||||||||||||||||
| 53 | 10 | ▶ | my $rv = $self->SUPER::CLOSE() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | or croak "couldn't close handle: $!"; | ||||||||||||||||||||||||||||
| 55 | 8 | return $rv; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { # the other file is closed, trigger the replacement now | ||||||||||||||||||||||||||||
| 58 | 11 | return !!$self->{repl}->finish } | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | sub UNTIE { | ||||||||||||||||||||||||||
|
Subroutine
UNTIE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | 2 | warnings::warnif("Please don't untie ".ref($self)." handles"); | |||||||||||||||||||||||||||
| 64 | 2 | $self->{other} = undef; | |||||||||||||||||||||||||||
| 65 | 2 | $self->{repl} = undef; | |||||||||||||||||||||||||||
| 66 | 2 | $self->SUPER::UNTIE(@_); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 33 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 33 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | # File::Replace destructor will warn on unclosed file | ||||||||||||||||||||||||||||
| 72 | 33 | $self->{other} = undef; | |||||||||||||||||||||||||||
| 73 | 33 | $self->{repl} = undef; | |||||||||||||||||||||||||||
| 74 | 33 | $self->SUPER::DESTROY(@_); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | 1; | |||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||