Merge branch 'master' of http://repo.or.cz/r/msysgit into devel
[msysgit/historical-msysgit.git] / lib / perl5 / 5.6.1 / Tie / Handle.pm
blob81b079224964ca152bc663954d16235966655cdb
1 package Tie::Handle;
3 use 5.005_64;
4 our $VERSION = '4.0';
6 =head1 NAME
8 Tie::Handle, Tie::StdHandle - base class definitions for tied handles
10 =head1 SYNOPSIS
12 package NewHandle;
13 require Tie::Handle;
15 @ISA = (Tie::Handle);
17 sub READ { ... } # Provide a needed method
18 sub TIEHANDLE { ... } # Overrides inherited method
21 package main;
23 tie *FH, 'NewHandle';
25 =head1 DESCRIPTION
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:
36 =over
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>.
61 =item READLINE this
63 Read a single line
65 =item GETC this
67 Get a single character
69 =item CLOSE this
71 Close the handle
73 =item OPEN this, filename
75 (Re-)open the handle
77 =item BINMODE this
79 Specify content is binary
81 =item EOF this
83 Test for end of file.
85 =item TELL this
87 Return position in the file.
89 =item SEEK this, offset, whence
91 Position the file.
93 Test for end of file.
95 =item DESTROY this
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.
102 =back
104 =head1 MORE INFORMATION
106 The L<perltie> section contains an example of tying handles.
108 =head1 COMPATIBILITY
110 This version of Tie::Handle is neither related to nor compatible with
111 the Tie::Handle (3.0) module available on CPAN. It was due to an
112 accident that two modules with the same name appeared. The namespace
113 clash has been cleared in favor of this module that comes with the
114 perl core in September 2000 and accordingly the version number has
115 been bumped up to 4.0.
117 =cut
119 use Carp;
120 use warnings::register;
122 sub new {
123 my $pkg = shift;
124 $pkg->TIEHANDLE(@_);
127 # "Grandfather" the new, a la Tie::Hash
129 sub TIEHANDLE {
130 my $pkg = shift;
131 if (defined &{"{$pkg}::new"}) {
132 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHANDLE is missing");
133 $pkg->new(@_);
135 else {
136 croak "$pkg doesn't define a TIEHANDLE method";
140 sub PRINT {
141 my $self = shift;
142 if($self->can('WRITE') != \&WRITE) {
143 my $buf = join(defined $, ? $, : "",@_);
144 $buf .= $\ if defined $\;
145 $self->WRITE($buf,length($buf),0);
147 else {
148 croak ref($self)," doesn't define a PRINT method";
152 sub PRINTF {
153 my $self = shift;
155 if($self->can('WRITE') != \&WRITE) {
156 my $buf = sprintf(shift,@_);
157 $self->WRITE($buf,length($buf),0);
159 else {
160 croak ref($self)," doesn't define a PRINTF method";
164 sub READLINE {
165 my $pkg = ref $_[0];
166 croak "$pkg doesn't define a READLINE method";
169 sub GETC {
170 my $self = shift;
172 if($self->can('READ') != \&READ) {
173 my $buf;
174 $self->READ($buf,1);
175 return $buf;
177 else {
178 croak ref($self)," doesn't define a GETC method";
182 sub READ {
183 my $pkg = ref $_[0];
184 croak "$pkg doesn't define a READ method";
187 sub WRITE {
188 my $pkg = ref $_[0];
189 croak "$pkg doesn't define a WRITE method";
192 sub CLOSE {
193 my $pkg = ref $_[0];
194 croak "$pkg doesn't define a CLOSE method";
197 package Tie::StdHandle;
198 our @ISA = 'Tie::Handle';
199 use Carp;
201 sub TIEHANDLE
203 my $class = shift;
204 my $fh = do { \local *HANDLE};
205 bless $fh,$class;
206 $fh->OPEN(@_) if (@_);
207 return $fh;
210 sub EOF { eof($_[0]) }
211 sub TELL { tell($_[0]) }
212 sub FILENO { fileno($_[0]) }
213 sub SEEK { seek($_[0],$_[1],$_[2]) }
214 sub CLOSE { close($_[0]) }
215 sub BINMODE { binmode($_[0]) }
217 sub OPEN
219 $_[0]->CLOSE if defined($_[0]->FILENO);
220 @_ == 2 ? open($_[0], $_[1]) : open($_[0], $_[1], $_[2]);
223 sub READ { read($_[0],$_[1],$_[2]) }
224 sub READLINE { my $fh = $_[0]; <$fh> }
225 sub GETC { getc($_[0]) }
227 sub WRITE
229 my $fh = $_[0];
230 print $fh substr($_[1],0,$_[2])