small apidoc fix
[p5sagit/p5-mst-13.2.git] / lib / Tie / Handle.pm
CommitLineData
1d603a67 1package Tie::Handle;
2
17f410f9 3use 5.005_64;
4our $VERSION = '1.0';
8cd2b3b0 5
1d603a67 6=head1 NAME
7
4592e6ca 8Tie::Handle, Tie::StdHandle - base class definitions for tied handles
1d603a67 9
10=head1 SYNOPSIS
11
12 package NewHandle;
13 require Tie::Handle;
3cb6de81 14
1d603a67 15 @ISA = (Tie::Handle);
3cb6de81 16
1d603a67 17 sub READ { ... } # Provide a needed method
18 sub TIEHANDLE { ... } # Overrides inherited method
3cb6de81 19
20
1d603a67 21 package main;
3cb6de81 22
1d603a67 23 tie *FH, 'NewHandle';
24
25=head1 DESCRIPTION
26
27This module provides some skeletal methods for handle-tying classes. See
28L<perltie> for a list of the functions required in tying a handle to a package.
29The basic B<Tie::Handle> package provides a C<new> method, as well as methods
4592e6ca 30C<TIEHANDLE>, C<PRINT>, C<PRINTF> and C<GETC>.
1d603a67 31
32For developers wishing to write their own tied-handle classes, the methods
33are summarized below. The L<perltie> section not only documents these, but
34has sample code as well:
35
36=over
37
38=item TIEHANDLE classname, LIST
39
40The method invoked by the command C<tie *glob, classname>. Associates a new
41glob instance with the specified class. C<LIST> would represent additional
42arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
43complete the association.
44
45=item WRITE this, scalar, length, offset
46
47Write I<length> bytes of data from I<scalar> starting at I<offset>.
48
49=item PRINT this, LIST
50
51Print the values in I<LIST>
52
53=item PRINTF this, format, LIST
54
55Print the values in I<LIST> using I<format>
56
57=item READ this, scalar, length, offset
58
59Read I<length> bytes of data into I<scalar> starting at I<offset>.
60
61=item READLINE this
62
63Read a single line
64
65=item GETC this
66
67Get a single character
68
8a059744 69=item CLOSE this
70
71Close the handle
72
4592e6ca 73=item OPEN this, filename
74
75(Re-)open the handle
76
77=item BINMODE this
78
79Specify content is binary
80
81=item EOF this
82
83Test for end of file.
84
85=item TELL this
86
87Return position in the file.
88
89=item SEEK this, offset, whence
90
91Position the file.
92
93Test for end of file.
94
1d603a67 95=item DESTROY this
96
97Free the storage associated with the tied handle referenced by I<this>.
98This is rarely needed, as Perl manages its memory quite well. But the
99option exists, should a class wish to perform specific actions upon the
100destruction of an instance.
101
102=back
103
104=head1 MORE INFORMATION
105
106The L<perltie> section contains an example of tying handles.
107
108=cut
109
110use Carp;
d3a7d8c7 111use warnings::register;
1d603a67 112
113sub new {
114 my $pkg = shift;
115 $pkg->TIEHANDLE(@_);
116}
117
118# "Grandfather" the new, a la Tie::Hash
119
120sub TIEHANDLE {
121 my $pkg = shift;
122 if (defined &{"{$pkg}::new"}) {
7e6d00f8 123 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHANDLE is missing");
1d603a67 124 $pkg->new(@_);
125 }
126 else {
127 croak "$pkg doesn't define a TIEHANDLE method";
128 }
129}
130
131sub PRINT {
132 my $self = shift;
133 if($self->can('WRITE') != \&WRITE) {
134 my $buf = join(defined $, ? $, : "",@_);
135 $buf .= $\ if defined $\;
136 $self->WRITE($buf,length($buf),0);
137 }
138 else {
139 croak ref($self)," doesn't define a PRINT method";
140 }
141}
142
143sub PRINTF {
144 my $self = shift;
145
146 if($self->can('WRITE') != \&WRITE) {
4592e6ca 147 my $buf = sprintf(shift,@_);
1d603a67 148 $self->WRITE($buf,length($buf),0);
149 }
150 else {
151 croak ref($self)," doesn't define a PRINTF method";
152 }
153}
154
155sub READLINE {
156 my $pkg = ref $_[0];
157 croak "$pkg doesn't define a READLINE method";
158}
159
160sub GETC {
161 my $self = shift;
162
163 if($self->can('READ') != \&READ) {
164 my $buf;
165 $self->READ($buf,1);
166 return $buf;
167 }
168 else {
169 croak ref($self)," doesn't define a GETC method";
170 }
171}
172
173sub READ {
174 my $pkg = ref $_[0];
175 croak "$pkg doesn't define a READ method";
176}
177
178sub WRITE {
179 my $pkg = ref $_[0];
180 croak "$pkg doesn't define a WRITE method";
181}
182
183sub CLOSE {
184 my $pkg = ref $_[0];
185 croak "$pkg doesn't define a CLOSE method";
4592e6ca 186}
187
188package Tie::StdHandle;
17f410f9 189our @ISA = 'Tie::Handle';
4592e6ca 190use Carp;
191
192sub TIEHANDLE
193{
194 my $class = shift;
195 my $fh = do { \local *HANDLE};
196 bless $fh,$class;
197 $fh->OPEN(@_) if (@_);
198 return $fh;
199}
200
201sub EOF { eof($_[0]) }
202sub TELL { tell($_[0]) }
203sub FILENO { fileno($_[0]) }
204sub SEEK { seek($_[0],$_[1],$_[2]) }
205sub CLOSE { close($_[0]) }
206sub BINMODE { binmode($_[0]) }
207
208sub OPEN
209{
210 $_[0]->CLOSE if defined($_[0]->FILENO);
211 open($_[0],$_[1]);
1d603a67 212}
213
4592e6ca 214sub READ { read($_[0],$_[1],$_[2]) }
215sub READLINE { my $fh = $_[0]; <$fh> }
216sub GETC { getc($_[0]) }
217
218sub WRITE
219{
220 my $fh = $_[0];
221 print $fh substr($_[1],0,$_[2])
222}
223
224
1d603a67 2251;