8 Tie::Handle, Tie::StdHandle - base class definitions for tied handles
17 sub READ { ... } # Provide a needed method
18 sub TIEHANDLE { ... } # Overrides inherited method
27 This module provides some skeletal methods for handle-tying classes. See
28 L<perltie> for a list of the functions required in tying a handle to a package.
29 The basic B<Tie::Handle> package provides a C<new> method, as well as methods
30 C<TIEHANDLE>, C<PRINT>, C<PRINTF> and C<GETC>.
32 For developers wishing to write their own tied-handle classes, the methods
33 are summarized below. The L<perltie> section not only documents these, but
34 has sample code as well:
38 =item TIEHANDLE classname, LIST
40 The method invoked by the command C<tie *glob, classname>. Associates a new
41 glob instance with the specified class. C<LIST> would represent additional
42 arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
43 complete the association.
45 =item WRITE this, scalar, length, offset
47 Write I<length> bytes of data from I<scalar> starting at I<offset>.
49 =item PRINT this, LIST
51 Print the values in I<LIST>
53 =item PRINTF this, format, LIST
55 Print the values in I<LIST> using I<format>
57 =item READ this, scalar, length, offset
59 Read I<length> bytes of data into I<scalar> starting at I<offset>.
67 Get a single character
73 =item OPEN this, filename
79 Specify content is binary
87 Return position in the file.
89 =item SEEK this, offset, whence
97 Free the storage associated with the tied handle referenced by I<this>.
98 This is rarely needed, as Perl manages its memory quite well. But the
99 option exists, should a class wish to perform specific actions upon the
100 destruction of an instance.
104 =head1 MORE INFORMATION
106 The L<perltie> section contains an example of tying handles.
111 use warnings::register;
118 # "Grandfather" the new, a la Tie::Hash
122 if (defined &{"{$pkg}::new"}) {
123 warnings::warn "WARNING: calling ${pkg}->new since ${pkg}->TIEHANDLE is missing"
124 if warnings::enabled();
128 croak "$pkg doesn't define a TIEHANDLE method";
134 if($self->can('WRITE') != \&WRITE) {
135 my $buf = join(defined $, ? $, : "",@_);
136 $buf .= $\ if defined $\;
137 $self->WRITE($buf,length($buf),0);
140 croak ref($self)," doesn't define a PRINT method";
147 if($self->can('WRITE') != \&WRITE) {
148 my $buf = sprintf(shift,@_);
149 $self->WRITE($buf,length($buf),0);
152 croak ref($self)," doesn't define a PRINTF method";
158 croak "$pkg doesn't define a READLINE method";
164 if($self->can('READ') != \&READ) {
170 croak ref($self)," doesn't define a GETC method";
176 croak "$pkg doesn't define a READ method";
181 croak "$pkg doesn't define a WRITE method";
186 croak "$pkg doesn't define a CLOSE method";
189 package Tie::StdHandle;
190 our @ISA = 'Tie::Handle';
196 my $fh = do { \local *HANDLE};
198 $fh->OPEN(@_) if (@_);
202 sub EOF { eof($_[0]) }
203 sub TELL { tell($_[0]) }
204 sub FILENO { fileno($_[0]) }
205 sub SEEK { seek($_[0],$_[1],$_[2]) }
206 sub CLOSE { close($_[0]) }
207 sub BINMODE { binmode($_[0]) }
211 $_[0]->CLOSE if defined($_[0]->FILENO);
215 sub READ { read($_[0],$_[1],$_[2]) }
216 sub READLINE { my $fh = $_[0]; <$fh> }
217 sub GETC { getc($_[0]) }
222 print $fh substr($_[1],0,$_[2])