File Coverage

blib/lib/IO/Cat.pm
Criterion Covered Total %
statement 41 43 95.3
branch 5 8 62.5
condition n/a
subroutine 8 8 100.0
pod 0 4 0.0
total 54 63 85.7


line stmt bran cond sub pod time code
1             package IO::Cat;
2              
3 1     1   670 use strict;
  1         2  
  1         31  
4 1     1   966 use IO::File;
  1         13006  
  1         198  
5 1     1   11 use Carp;
  1         7  
  1         56  
6 1     1   5 use vars qw($VERSION);
  1         1  
  1         536  
7              
8             $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
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             sub new {
47 1     1 0 375 my ($class, $file) = @_;
48 1         2 my $self = {};
49              
50 1         3 bless $self, $class;
51 1 50       11 $self->file( $file ) if defined $file;
52            
53 1         2 return $self;
54             }
55              
56              
57             =item *
58              
59             file I
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             sub file {
68 3     3 0 6 my $self = shift;
69              
70 3 100       10 if (@_) {
71 1 50       9 if ($self->{fh}) {
72 0         0 $self->{fh}->close();
73             }
74            
75 1         4 $self->{file} = $_[0];
76 1         12 $self->{fh} = IO::File->new( $_[0] );
77 1 50       97 unless ($self->{fh}) {
78 0         0 croak "Can't open file $_[0]: $!";
79             }
80             }
81              
82 3         8 return $self->{fh};
83             }
84              
85             =item *
86              
87             cat I
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             sub cat ($) {
96 1     1 0 6 my ($self, $output) = @_;
97 1         3 my $input = $self->file();
98            
99 1         15 while (<$input>) {
100 4         33 print $output $_;
101             }
102 1         15 $input->seek( 0, 0 );
103            
104 1         12 return( 1 );
105             }
106              
107              
108              
109             =pod
110              
111             =item *
112              
113             cattail I
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             sub cattail ($) {
123 1     1 0 215 my ($self, $output) = @_;
124 1         3 my $input = $self->file();
125 1         3 my @lines = (0);
126              
127 1         11 while (<$input>) {
128 4         66 $lines[$.] = $input->tell();
129             }
130              
131 1         11 pop @lines;
132 1         4 while (defined ($_ = pop @lines)) {
133 3         10 $input->seek( $_, 0 );
134 3         44 print $output scalar(<$input>);
135             }
136 1         4 $input->seek( 0, 0 );
137              
138 1         12 return (1);
139             }
140              
141              
142              
143             =back
144              
145             =head1 AUTHOR
146              
147             Dennis Taylor, Ecorbeau@execpc.comE
148              
149             =head1 SEE ALSO
150              
151             cat(1) and the File::Cat module.
152              
153             =cut
154              
155              
156             1;