File Coverage

blib/lib/Tie/FileHandle/Base.pm
Criterion Covered Total %
statement 6 44 13.6
branch 0 18 0.0
condition 0 5 0.0
subroutine 2 10 20.0
pod n/a
total 8 77 10.3


line stmt bran cond sub pod time code
1             #!/usr/local/bin/perl
2            
3             =head1 NAME
4            
5             Tie::FileHandle::Base - a base class to simplify filehandle tie module implementation
6            
7             =head1 DESCRIPTION
8            
9             By noting the redundancies inherent in the filehandle tie methods, this
10             module seeks to aid in implementation of new modules by reducing the number
11             of required functions.
12            
13             Care should be taken by classes that use AUTOLOAD. Make sure to predeclare
14             subroutines that will be autoloaded - as in:
15            
16             sub PRINT;
17            
18             Otherwise this module will make incorrect presumptions and your module will
19             not function as you intend.
20            
21             =head2 OUTPUT FUNCTIONS
22            
23             Since PRINT, PRINTF, and WRITE are all quite similar in scope, any one of
24             these can be implemented from any of the others. So, you only need implement
25             one of the above.
26            
27             =head2 INPUT FUNCTIONS
28            
29             By implementing READ or GETC, you can get the entire complement of READ,
30             READLINE, and GETC. Note however that READ and GETC cannot be derived
31             nicely from READLINE.
32            
33             =head2 OTHERS
34            
35             EOF can be implemented crudely if given READ or GETC along
36             with a backwards supporting SEEK.
37            
38             =head1 HISTORY
39            
40             =over 2
41            
42             =item *
43            
44             03/09/02 - Robby Walker - did the output stuff - version 0.1
45            
46             =item *
47            
48             02/13/02 - Robby Walker - created the file - version 0.001
49            
50             =back
51            
52             =head1 METHODS
53            
54             =over 4
55            
56             =cut
57             #----------------------------------------------------------
58            
59             package Tie::FileHandle::Base;
60            
61 1     1   10694 use vars qw($VERSION %loop);
  1         3  
  1         62  
62 1     1   6 use strict;
  1         2  
  1         633  
63            
64             $VERSION = 0.1;
65             %loop = ();
66            
67             # ------------------------------------------------------------------------
68             # METHOD: _fh_error
69             # ------------------------------------------------------------------------
70             # Our replacement for 'croak' or 'die' that errors to the proper level
71             sub _fh_error {
72 0     0     my $error_string = shift;
73 0           my $i = 1;
74 0           while (my ($package, $filename, $line, $subroutine,
75             undef, undef, undef, undef, undef, undef) = caller($i++) )
76             {
77 0 0         if ( $package ne 'Tie::FileHandle::Base' ) {
78 0           $subroutine =~ s/.*\:([^:]+)/$1/;
79 0           die "Cannot execute filehandle method $subroutine : $error_string at $filename line $line\n";
80             }
81             }
82             }
83            
84             # ------------------------------------------------------------------------
85             # METHOD: PRINT
86             # ------------------------------------------------------------------------
87            
88             =item PRINT
89            
90             Implements PRINT based on WRITE or PRINTF.
91            
92             =cut
93             sub PRINT {
94 0     0     my $self = shift;
95 0           my $result = 0;
96            
97             # guard against loops
98 0 0         _fh_error( "function not defined" ) if ( $loop{$self} );
99 0           $loop{$self} = 1;
100            
101 0 0         if ( $self->can('WRITE') != \&WRITE ) {
    0          
102             # loop over the strings
103 0           $result = 1;
104 0           foreach my $str ( @_ ) {
105             # print each string carefully
106 0           my $offset = 0;
107 0           my $ln = length( $str );
108             # loop until all characters are printed
109 0           while ( $offset != $ln ) {
110 0           my $ret = $self->WRITE( $str, $ln - $offset, $offset );
111 0 0         unless ( $ret ) {
112 0           $result = undef;
113 0           last;
114             }
115             };
116             # see if we exited early
117 0 0         last if $offset != $ln;
118             }
119            
120             } elsif ( $self->can('PRINTF') != \&PRINTF ) {
121 0           $result = $self->PRINTF( '%s' x (@_+0), @_ );
122            
123             } else {
124 0           _fh_error( "function not defined" );
125             }
126            
127 0           $loop{$self} = 0;
128 0           1;
129             }
130            
131             # ------------------------------------------------------------------------
132             # METHOD: PRINTF
133             # ------------------------------------------------------------------------
134            
135             =item PRINTF
136            
137             Implements PRINTF based off of PRINT, which may in turn base itself off of WRITE.
138            
139             =cut
140             sub PRINTF {
141 0     0     ( shift )->PRINT( sprintf @_[1..$#_] );
142             }
143            
144             # ------------------------------------------------------------------------
145             # METHOD: WRITE
146             # ------------------------------------------------------------------------
147            
148             =item WRITE
149            
150             Implements WRITE based off of PRINT, which may in turn base itself off of PRINTF.
151            
152             =cut
153             sub WRITE {
154 0     0     my ($self, $var, $length, $offset) = @_;
155 0   0       my $ln = $length || length( $var );
156 0 0 0       $self->PRINT( substr $var, $offset || 0, $ln ) && $ln;
157             }
158            
159             # ------------------------------------------------------------------------
160             # METHOD: GETC
161             # ------------------------------------------------------------------------
162            
163             =item GETC
164            
165             =cut
166 0     0     sub GETC {
167            
168             }
169            
170             # ------------------------------------------------------------------------
171             # METHOD: READ
172             # ------------------------------------------------------------------------
173            
174             =item READ
175            
176             =cut
177 0     0     sub READ {
178            
179             }
180            
181             # ------------------------------------------------------------------------
182             # METHOD: READLINE
183             # ------------------------------------------------------------------------
184            
185             =item READLINE
186            
187             =cut
188 0     0     sub READLINE {
189            
190             }
191            
192             # ------------------------------------------------------------------------
193             # METHOD: EOF
194             # ------------------------------------------------------------------------
195            
196             =item EOF
197            
198             Crude EOF implemented using READ and SEEK.
199            
200             =cut
201             sub EOF {
202 0     0     my $self = shift;
203 0 0         if ( $self->can('SEEK') ) {
204 0           my $temp;
205             # test EOF by reading
206 0 0         return 1 unless $self->READ( $temp, 1 );
207 0           $self->SEEK( 1, -1 ); # go back to where we were
208             }
209 0           return 0;
210             }
211            
212            
213            
214             1;
215            
216             __END__