| 1 | package IO::Cat; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | $VERSION = '1.01'; | |||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | IO::Cat - Object-oriented Perl implementation of cat(1) | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | require IO::Cat; | ||||||||||
| 19 | |||||||||||
| 20 | my $meow = new IO::Cat '/etc/motd'; | ||||||||||
| 21 | $meow->cat( \*STDOUT, \*STDERR ) | ||||||||||
| 22 | or die "Can't cat /etc/motd: $!"; | ||||||||||
| 23 | |||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||
| 25 | |||||||||||
| 26 | IO::Cat provides an intuitive, scalable, encapsulated interface to the | ||||||||||
| 27 | common task of printing to a filehandle. Use it a few times, and you'll | ||||||||||
| 28 | never know how you lived without it! | ||||||||||
| 29 | |||||||||||
| 30 | =head1 METHODS | ||||||||||
| 31 | |||||||||||
| 32 | =over | ||||||||||
| 33 | |||||||||||
| 34 | =item * | ||||||||||
| 35 | |||||||||||
| 36 | new I<FILENAME> | ||||||||||
| 37 | |||||||||||
| 38 | This constructor takes the name of a file to be catted and returns a | ||||||||||
| 39 | brand spanking new IO::Cat object. If you prefer, you can pass it no | ||||||||||
| 40 | args here and use the file() accessor method to set the filename | ||||||||||
| 41 | before calling cat(). | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | #' | ||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 47 | 1 | my ($class, $file) = @_; | |||||||||
| 48 | 1 | my $self = {}; | |||||||||
| 49 | |||||||||||
| 50 | 1 | bless $self, $class; | |||||||||
| 51 | 1 | ▶ | $self->file( $file ) if defined $file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 1 | return $self; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | =item * | ||||||||||
| 58 | |||||||||||
| 59 | file I<FILENAME> | ||||||||||
| 60 | |||||||||||
| 61 | An accessor method that lets you set the filename or filehandle which | ||||||||||
| 62 | a particular IO::Cat object will cat. Returns the open filehandle | ||||||||||
| 63 | which it will cat from. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 3 | ▶ | sub file { | ||||||||
|
Subroutine
file: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||
| 68 | 3 | my $self = shift; | |||||||||
| 69 | |||||||||||
| 70 | 3 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | ▶ | if ($self->{fh}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $self->{fh}->close(); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 1 | $self->{file} = $_[0]; | |||||||||
| 76 | 1 | $self->{fh} = IO::File->new( $_[0] ); | |||||||||
| 77 | 1 | ▶ | unless ($self->{fh}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | croak "Can't open file $_[0]: $!"; | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 3 | return $self->{fh}; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | =item * | ||||||||||
| 86 | |||||||||||
| 87 | cat I<FILEHANDLE> | ||||||||||
| 88 | |||||||||||
| 89 | Copies data from a previously specified file to FILEHANDLE, or returns | ||||||||||
| 90 | false if an error occurred. | ||||||||||
| 91 | |||||||||||
| 92 | =cut | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | 1 | ▶ | sub cat ($) { | ||||||||
|
Subroutine
cat: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 96 | 1 | my ($self, $output) = @_; | |||||||||
| 97 | 1 | my $input = $self->file(); | |||||||||
| 98 | |||||||||||
| 99 | 1 | ▶ | while (<$input>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 4 | print $output $_; | |||||||||
| 101 | } | ||||||||||
| 102 | 1 | $input->seek( 0, 0 ); | |||||||||
| 103 | |||||||||||
| 104 | 1 | return( 1 ); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | =pod | ||||||||||
| 110 | |||||||||||
| 111 | =item * | ||||||||||
| 112 | |||||||||||
| 113 | cattail I<FILEHANDLE> | ||||||||||
| 114 | |||||||||||
| 115 | Prints data from a previously specified file to FILEHANDLE -- | ||||||||||
| 116 | backwards, line by line -- or returns false if an error occurred. | ||||||||||
| 117 | |||||||||||
| 118 | =cut | ||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | 1 | ▶ | sub cattail ($) { | ||||||||
|
Subroutine
cattail: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 123 | 1 | my ($self, $output) = @_; | |||||||||
| 124 | 1 | my $input = $self->file(); | |||||||||
| 125 | 1 | my @lines = (0); | |||||||||
| 126 | |||||||||||
| 127 | 1 | ▶ | while (<$input>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 4 | $lines[$.] = $input->tell(); | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 1 | pop @lines; | |||||||||
| 132 | 1 | ▶ | while (defined ($_ = pop @lines)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 3 | $input->seek( $_, 0 ); | |||||||||
| 134 | 3 | print $output scalar(<$input>); | |||||||||
| 135 | } | ||||||||||
| 136 | 1 | $input->seek( 0, 0 ); | |||||||||
| 137 | |||||||||||
| 138 | 1 | return (1); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | =back | ||||||||||
| 144 | |||||||||||
| 145 | =head1 AUTHOR | ||||||||||
| 146 | |||||||||||
| 147 | Dennis Taylor, E<lt>corbeau@execpc.comE<gt> | ||||||||||
| 148 | |||||||||||
| 149 | =head1 SEE ALSO | ||||||||||
| 150 | |||||||||||
| 151 | cat(1) and the File::Cat module. | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | |||||||||||
| 156 | 1 | 1; | |||||||||