Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
4.8 KiB

  1. package Tie::Handle;
  2. use 5.005_64;
  3. our $VERSION = '4.0';
  4. =head1 NAME
  5. Tie::Handle, Tie::StdHandle - base class definitions for tied handles
  6. =head1 SYNOPSIS
  7. package NewHandle;
  8. require Tie::Handle;
  9. @ISA = (Tie::Handle);
  10. sub READ { ... } # Provide a needed method
  11. sub TIEHANDLE { ... } # Overrides inherited method
  12. package main;
  13. tie *FH, 'NewHandle';
  14. =head1 DESCRIPTION
  15. This module provides some skeletal methods for handle-tying classes. See
  16. L<perltie> for a list of the functions required in tying a handle to a package.
  17. The basic B<Tie::Handle> package provides a C<new> method, as well as methods
  18. C<TIEHANDLE>, C<PRINT>, C<PRINTF> and C<GETC>.
  19. For developers wishing to write their own tied-handle classes, the methods
  20. are summarized below. The L<perltie> section not only documents these, but
  21. has sample code as well:
  22. =over
  23. =item TIEHANDLE classname, LIST
  24. The method invoked by the command C<tie *glob, classname>. Associates a new
  25. glob instance with the specified class. C<LIST> would represent additional
  26. arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
  27. complete the association.
  28. =item WRITE this, scalar, length, offset
  29. Write I<length> bytes of data from I<scalar> starting at I<offset>.
  30. =item PRINT this, LIST
  31. Print the values in I<LIST>
  32. =item PRINTF this, format, LIST
  33. Print the values in I<LIST> using I<format>
  34. =item READ this, scalar, length, offset
  35. Read I<length> bytes of data into I<scalar> starting at I<offset>.
  36. =item READLINE this
  37. Read a single line
  38. =item GETC this
  39. Get a single character
  40. =item CLOSE this
  41. Close the handle
  42. =item OPEN this, filename
  43. (Re-)open the handle
  44. =item BINMODE this
  45. Specify content is binary
  46. =item EOF this
  47. Test for end of file.
  48. =item TELL this
  49. Return position in the file.
  50. =item SEEK this, offset, whence
  51. Position the file.
  52. Test for end of file.
  53. =item DESTROY this
  54. Free the storage associated with the tied handle referenced by I<this>.
  55. This is rarely needed, as Perl manages its memory quite well. But the
  56. option exists, should a class wish to perform specific actions upon the
  57. destruction of an instance.
  58. =back
  59. =head1 MORE INFORMATION
  60. The L<perltie> section contains an example of tying handles.
  61. =head1 COMPATIBILITY
  62. This version of Tie::Handle is neither related to nor compatible with
  63. the Tie::Handle (3.0) module available on CPAN. It was due to an
  64. accident that two modules with the same name appeared. The namespace
  65. clash has been cleared in favor of this module that comes with the
  66. perl core in September 2000 and accordingly the version number has
  67. been bumped up to 4.0.
  68. =cut
  69. use Carp;
  70. use warnings::register;
  71. sub new {
  72. my $pkg = shift;
  73. $pkg->TIEHANDLE(@_);
  74. }
  75. # "Grandfather" the new, a la Tie::Hash
  76. sub TIEHANDLE {
  77. my $pkg = shift;
  78. if (defined &{"{$pkg}::new"}) {
  79. warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHANDLE is missing");
  80. $pkg->new(@_);
  81. }
  82. else {
  83. croak "$pkg doesn't define a TIEHANDLE method";
  84. }
  85. }
  86. sub PRINT {
  87. my $self = shift;
  88. if($self->can('WRITE') != \&WRITE) {
  89. my $buf = join(defined $, ? $, : "",@_);
  90. $buf .= $\ if defined $\;
  91. $self->WRITE($buf,length($buf),0);
  92. }
  93. else {
  94. croak ref($self)," doesn't define a PRINT method";
  95. }
  96. }
  97. sub PRINTF {
  98. my $self = shift;
  99. if($self->can('WRITE') != \&WRITE) {
  100. my $buf = sprintf(shift,@_);
  101. $self->WRITE($buf,length($buf),0);
  102. }
  103. else {
  104. croak ref($self)," doesn't define a PRINTF method";
  105. }
  106. }
  107. sub READLINE {
  108. my $pkg = ref $_[0];
  109. croak "$pkg doesn't define a READLINE method";
  110. }
  111. sub GETC {
  112. my $self = shift;
  113. if($self->can('READ') != \&READ) {
  114. my $buf;
  115. $self->READ($buf,1);
  116. return $buf;
  117. }
  118. else {
  119. croak ref($self)," doesn't define a GETC method";
  120. }
  121. }
  122. sub READ {
  123. my $pkg = ref $_[0];
  124. croak "$pkg doesn't define a READ method";
  125. }
  126. sub WRITE {
  127. my $pkg = ref $_[0];
  128. croak "$pkg doesn't define a WRITE method";
  129. }
  130. sub CLOSE {
  131. my $pkg = ref $_[0];
  132. croak "$pkg doesn't define a CLOSE method";
  133. }
  134. package Tie::StdHandle;
  135. our @ISA = 'Tie::Handle';
  136. use Carp;
  137. sub TIEHANDLE
  138. {
  139. my $class = shift;
  140. my $fh = do { \local *HANDLE};
  141. bless $fh,$class;
  142. $fh->OPEN(@_) if (@_);
  143. return $fh;
  144. }
  145. sub EOF { eof($_[0]) }
  146. sub TELL { tell($_[0]) }
  147. sub FILENO { fileno($_[0]) }
  148. sub SEEK { seek($_[0],$_[1],$_[2]) }
  149. sub CLOSE { close($_[0]) }
  150. sub BINMODE { binmode($_[0]) }
  151. sub OPEN
  152. {
  153. $_[0]->CLOSE if defined($_[0]->FILENO);
  154. @_ == 2 ? open($_[0], $_[1]) : open($_[0], $_[1], $_[2]);
  155. }
  156. sub READ { read($_[0],$_[1],$_[2]) }
  157. sub READLINE { my $fh = $_[0]; <$fh> }
  158. sub GETC { getc($_[0]) }
  159. sub WRITE
  160. {
  161. my $fh = $_[0];
  162. print $fh substr($_[1],0,$_[2])
  163. }
  164. 1;