xref: /openbsd-src/gnu/usr.bin/perl/lib/Tie/StdHandle.pm (revision de8cc8edbc71bd3e3bc7fbffa27ba0e564c37d8b)
1package Tie::StdHandle;
2
3use strict;
4
5use Tie::Handle;
6our @ISA = 'Tie::Handle';
7our $VERSION = '4.6';
8
9=head1 NAME
10
11Tie::StdHandle - base class definitions for tied handles
12
13=head1 SYNOPSIS
14
15    package NewHandle;
16    require Tie::Handle;
17
18    @ISA = qw(Tie::Handle);
19
20    sub READ { ... }		# Provide a needed method
21    sub TIEHANDLE { ... }	# Overrides inherited method
22
23
24    package main;
25
26    tie *FH, 'NewHandle';
27
28=head1 DESCRIPTION
29
30The B<Tie::StdHandle> package provide most methods for file handles described
31in L<perltie> (the exceptions are C<UNTIE> and C<DESTROY>).  It causes tied
32file handles to behave exactly like standard file handles and allow for
33selective overwriting of methods.
34
35=cut
36
37sub TIEHANDLE
38{
39 my $class = shift;
40 my $fh    = \do { local *HANDLE};
41 bless $fh,$class;
42 $fh->OPEN(@_) if (@_);
43 return $fh;
44}
45
46sub EOF     { eof($_[0]) }
47sub TELL    { tell($_[0]) }
48sub FILENO  { fileno($_[0]) }
49sub SEEK    { seek($_[0],$_[1],$_[2]) }
50sub CLOSE   { close($_[0]) }
51sub BINMODE { &CORE::binmode(shift, @_) }
52
53sub OPEN
54{
55 $_[0]->CLOSE if defined($_[0]->FILENO);
56 @_ == 2 ? open($_[0], $_[1]) : open($_[0], $_[1], $_[2]);
57}
58
59sub READ     { &CORE::read(shift, \shift, @_) }
60sub READLINE { my $fh = $_[0]; <$fh> }
61sub GETC     { getc($_[0]) }
62
63sub WRITE
64{
65 my $fh = $_[0];
66 local $\; # don't print any line terminator
67 print $fh substr($_[1], $_[3], $_[2]);
68}
69
70
711;
72