| 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 | ▶ | use vars qw($VERSION %loop); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | $VERSION = 0.1; | |||||||||||||||||||
| 65 | 1 | %loop = (); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 68 | # METHOD: _fh_error | ||||||||||||||||||||
| 69 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 70 | # Our replacement for 'croak' or 'die' that errors to the proper level | ||||||||||||||||||||
| 71 | 0 | ▶ | sub _fh_error { | ||||||||||||||||||
|
Subroutine
_fh_error: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 72 | 0 | my $error_string = shift; | |||||||||||||||||||
| 73 | 0 | my $i = 1; | |||||||||||||||||||
| 74 | 0 | ▶ | while (my ($package, $filename, $line, $subroutine, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | undef, undef, undef, undef, undef, undef) = caller($i++) ) | ||||||||||||||||||||
| 76 | { | ||||||||||||||||||||
| 77 | 0 | ▶ | if ( $package ne 'Tie::FileHandle::Base' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub PRINT { | ||||||||||||||||||
|
Subroutine
PRINT: not called CC=8 SCAR=42.8
| |||||||||||||||||||||
| 94 | 0 | my $self = shift; | |||||||||||||||||||
| 95 | 0 | my $result = 0; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # guard against loops | ||||||||||||||||||||
| 98 | 0 | ▶ | _fh_error( "function not defined" ) if ( $loop{$self} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | $loop{$self} = 1; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | if ( $self->can('WRITE') != \&WRITE ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 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 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 0 | my $ret = $self->WRITE( $str, $ln - $offset, $offset ); | |||||||||||||||||||
| 111 | 0 | ▶ | unless ( $ret ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | $result = undef; | |||||||||||||||||||
| 113 | 0 | last; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||
| 116 | # see if we exited early | ||||||||||||||||||||
| 117 | 0 | ▶ | last if $offset != $ln; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub PRINTF { | ||||||||||||||||||
|
Subroutine
PRINTF: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 141 | 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 | 0 | ▶ | sub WRITE { | ||||||||||||||||||
|
Subroutine
WRITE: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 154 | 0 | my ($self, $var, $length, $offset) = @_; | |||||||||||||||||||
| 155 | 0 | ▶ | my $ln = $length || length( $var ); | ||||||||||||||||||
|
Condition0%0/3
$length || length $var
MC/DC0%0/2
$length || length $var
$length
length $var
Truth table0%0/3
A || B
A= $lengthB= length $var
| |||||||||||||||||||||
| 156 | 0 | ▶ | $self->PRINT( substr $var, $offset || 0, $ln ) && $ln; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$offset || 0
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $offset
| |||||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 160 | # METHOD: GETC | ||||||||||||||||||||
| 161 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =item GETC | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||
| 166 | 0 | ▶ | sub GETC { | ||||||||||||||||||
|
Subroutine
GETC: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 171 | # METHOD: READ | ||||||||||||||||||||
| 172 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =item READ | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||
| 177 | 0 | ▶ | sub READ { | ||||||||||||||||||
|
Subroutine
READ: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 182 | # METHOD: READLINE | ||||||||||||||||||||
| 183 | # ------------------------------------------------------------------------ | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =item READLINE | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||
| 188 | 0 | ▶ | sub READLINE { | ||||||||||||||||||
|
Subroutine
READLINE: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub EOF { | ||||||||||||||||||
|
Subroutine
EOF: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 202 | 0 | my $self = shift; | |||||||||||||||||||
| 203 | 0 | ▶ | if ( $self->can('SEEK') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | 0 | my $temp; | |||||||||||||||||||
| 205 | # test EOF by reading | ||||||||||||||||||||
| 206 | 0 | ▶ | return 1 unless $self->READ( $temp, 1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | 0 | $self->SEEK( 1, -1 ); # go back to where we were | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | 0 | return 0; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 1 | 1; | |||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | __END__ | ||||||||||||||||||||