| 1 | package Launcher::Cascade::FileReader::Seekable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Launcher::Cascade::FileReader::Seekable - a filereader that memorizes its position between reads. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Launcher::Cascade::FileReader::Seekable; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $f = new Launcher::Cascade::FileReader -path => ... ; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | my $fh1 = $f->open(); | ||||||||||||||||||||||||||||
| 14 | my $line1 = <$fh1>; # first line | ||||||||||||||||||||||||||||
| 15 | $f->close(); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # later, in a nearby piece of code | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my $fh2 = $f->open(); # different filehandle | ||||||||||||||||||||||||||||
| 20 | my $line2 = <$fh2>; # next line | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Launcher::Cascade::FileReader::Seekable inherits from | ||||||||||||||||||||||||||||
| 26 | Launcher::Cascade::FileReader but keeps in memory the position where it was at | ||||||||||||||||||||||||||||
| 27 | when the filehandle is closed. Subsequent calls to open() will perform the | ||||||||||||||||||||||||||||
| 28 | necessary operations to resume reading where it last stopped, be it when | ||||||||||||||||||||||||||||
| 29 | reading a local file, or a remote file through ssh. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | use base qw( Launcher::Cascade::FileReader ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head2 Attributes | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =over 4 | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item B<position> | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =back | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | 3 | Launcher::Cascade::make_accessors qw( position ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 Methods | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # _remote_cat: the command to launch remotely to read a file over ssh | ||||||||||||||||||||||||||||
| 56 | # either return a regular 'cat' if one wants to start from the start, or a Perl | ||||||||||||||||||||||||||||
| 57 | # one-liner to first seek the wanted position. | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub _remote_cat { | ||||||||||||||||||||||||||
|
Subroutine
_remote_cat: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 0 | my $path = shift; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my $pos = $self->position(); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $pos ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | return qq#perl -e "open IN,q{<},pop;seek IN,pop,0;print while <IN>" $pos $path# | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 0 | return $self->SUPER::_remote_cat($path); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =over 4 | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =item B<close> | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Stores the filehandle's position in the position() attribute, then closes the | ||||||||||||||||||||||||||||
| 78 | filehandle. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 1 | ▶ | $self->position(tell($self->filehandle()) + ($self->host() ? $self->position() || 0 : 0)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->position or 0
MC/DC0%0/1
$self->position or 0
$self->position
Truth table0%0/2
A
A= $self->position
| |||||||||||||||||||||||||||||
| 86 | 1 | $self->SUPER::close(); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item B<open> | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Opens the file (locally or remotely), seeks to the desired position() and | ||||||||||||||||||||||||||||
| 92 | returns a filehandle. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 99 | 2 | my $fh = $self->SUPER::open(); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | ▶ | if ( $self->position() && !$self->host() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->position and not $self->host
MC/DC50%1/2
$self->position and not $self->host
$self->position
not $self->host
Truth table66%2/3
A and not(B)
A= $self->positionB= not $self->host
| |||||||||||||||||||||||||||||
| 102 | 1 | seek $fh, $self->position(), 0; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 2 | return $fh; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 BUGS AND CAVEATS | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =over 4 | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item * | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | C<perl> has to be in a directory mentioned in the C<PATH> environment variable | ||||||||||||||||||||||||||||
| 116 | on the remote host, because the C<seek> operation on remote files is performed | ||||||||||||||||||||||||||||
| 117 | with a Perl one-liner. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =item * | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | The memorization of the position is performed only when closing the filehandle | ||||||||||||||||||||||||||||
| 122 | through the close() method of Launcher::Cascade::FileReader::Seekable, not when | ||||||||||||||||||||||||||||
| 123 | closing the filehandle directly: | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | my $fh = $f->open(); | ||||||||||||||||||||||||||||
| 126 | ... # do something with it | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | $f->close(); # right: position() is updated | ||||||||||||||||||||||||||||
| 129 | close $fh; # wrong: position() is *not* updated | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =back | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | L<Launcher::Cascade::FileReader> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Cédric Bouvier C<< <cbouvi@cpan.org> >> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Copyright (C) 2006 Cédric Bouvier, All Rights Reserved. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 146 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 3 | 1; # end of Launcher::Cascade::FileReader::Seekable | |||||||||||||||||||||||||||