Source code of Windows XP (NT5)
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.

161 lines
3.4 KiB

  1. package Tie::Handle;
  2. =head1 NAME
  3. Tie::Handle - base class definitions for tied handles
  4. =head1 SYNOPSIS
  5. package NewHandle;
  6. require Tie::Handle;
  7. @ISA = (Tie::Handle);
  8. sub READ { ... } # Provide a needed method
  9. sub TIEHANDLE { ... } # Overrides inherited method
  10. package main;
  11. tie *FH, 'NewHandle';
  12. =head1 DESCRIPTION
  13. This module provides some skeletal methods for handle-tying classes. See
  14. L<perltie> for a list of the functions required in tying a handle to a package.
  15. The basic B<Tie::Handle> package provides a C<new> method, as well as methods
  16. C<TIESCALAR>, C<FETCH> and C<STORE>. The C<new> method is provided as a means
  17. of grandfathering, for classes that forget to provide their own C<TIESCALAR>
  18. method.
  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 DESTROY this
  41. Free the storage associated with the tied handle referenced by I<this>.
  42. This is rarely needed, as Perl manages its memory quite well. But the
  43. option exists, should a class wish to perform specific actions upon the
  44. destruction of an instance.
  45. =back
  46. =head1 MORE INFORMATION
  47. The L<perltie> section contains an example of tying handles.
  48. =cut
  49. use Carp;
  50. sub new {
  51. my $pkg = shift;
  52. $pkg->TIEHANDLE(@_);
  53. }
  54. # "Grandfather" the new, a la Tie::Hash
  55. sub TIEHANDLE {
  56. my $pkg = shift;
  57. if (defined &{"{$pkg}::new"}) {
  58. carp "WARNING: calling ${pkg}->new since ${pkg}->TIEHANDLE is missing"
  59. if $^W;
  60. $pkg->new(@_);
  61. }
  62. else {
  63. croak "$pkg doesn't define a TIEHANDLE method";
  64. }
  65. }
  66. sub PRINT {
  67. my $self = shift;
  68. if($self->can('WRITE') != \&WRITE) {
  69. my $buf = join(defined $, ? $, : "",@_);
  70. $buf .= $\ if defined $\;
  71. $self->WRITE($buf,length($buf),0);
  72. }
  73. else {
  74. croak ref($self)," doesn't define a PRINT method";
  75. }
  76. }
  77. sub PRINTF {
  78. my $self = shift;
  79. if($self->can('WRITE') != \&WRITE) {
  80. my $buf = sprintf(@_);
  81. $self->WRITE($buf,length($buf),0);
  82. }
  83. else {
  84. croak ref($self)," doesn't define a PRINTF method";
  85. }
  86. }
  87. sub READLINE {
  88. my $pkg = ref $_[0];
  89. croak "$pkg doesn't define a READLINE method";
  90. }
  91. sub GETC {
  92. my $self = shift;
  93. if($self->can('READ') != \&READ) {
  94. my $buf;
  95. $self->READ($buf,1);
  96. return $buf;
  97. }
  98. else {
  99. croak ref($self)," doesn't define a GETC method";
  100. }
  101. }
  102. sub READ {
  103. my $pkg = ref $_[0];
  104. croak "$pkg doesn't define a READ method";
  105. }
  106. sub WRITE {
  107. my $pkg = ref $_[0];
  108. croak "$pkg doesn't define a WRITE method";
  109. }
  110. sub CLOSE {
  111. my $pkg = ref $_[0];
  112. croak "$pkg doesn't define a CLOSE method";
  113. }
  114. 1;