home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Tie / Scalar.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  3.1 KB  |  132 lines

  1. package Tie::Scalar;
  2.  
  3. =head1 NAME
  4.  
  5. Tie::Scalar, Tie::StdScalar - base class definitions for tied scalars
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.     package NewScalar;
  10.     require Tie::Scalar;
  11.      
  12.     @ISA = (Tie::Scalar);
  13.      
  14.     sub FETCH { ... }        # Provide a needed method
  15.     sub TIESCALAR { ... }    # Overrides inherited method
  16.          
  17.      
  18.     package NewStdScalar;
  19.     require Tie::Scalar;
  20.     
  21.     @ISA = (Tie::StdScalar);
  22.     
  23.     sub FETCH { ... }
  24.     
  25.     
  26.     package main;
  27.     
  28.     tie $new_scalar, 'NewScalar';
  29.     tie $new_std_scalar, 'NewStdScalar';
  30.  
  31. =head1 DESCRIPTION
  32.  
  33. This module provides some skeletal methods for scalar-tying classes. See
  34. L<perltie> for a list of the functions required in tying a scalar to a
  35. package. The basic B<Tie::Scalar> package provides a C<new> method, as well
  36. as methods C<TIESCALAR>, C<FETCH> and C<STORE>. The B<Tie::StdScalar>
  37. package provides all the methods specified in  L<perltie>. It inherits from
  38. B<Tie::Scalar> and causes scalars tied to it to behave exactly like the
  39. built-in scalars, allowing for selective overloading of methods. The C<new>
  40. method is provided as a means of grandfathering, for classes that forget to
  41. provide their own C<TIESCALAR> method.
  42.  
  43. For developers wishing to write their own tied-scalar classes, the methods
  44. are summarized below. The L<perltie> section not only documents these, but
  45. has sample code as well:
  46.  
  47. =over
  48.  
  49. =item TIESCALAR classname, LIST
  50.  
  51. The method invoked by the command C<tie $scalar, classname>. Associates a new
  52. scalar instance with the specified class. C<LIST> would represent additional
  53. arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
  54. complete the association.
  55.  
  56. =item FETCH this
  57.  
  58. Retrieve the value of the tied scalar referenced by I<this>.
  59.  
  60. =item STORE this, value
  61.  
  62. Store data I<value> in the tied scalar referenced by I<this>.
  63.  
  64. =item DESTROY this
  65.  
  66. Free the storage associated with the tied scalar referenced by I<this>.
  67. This is rarely needed, as Perl manages its memory quite well. But the
  68. option exists, should a class wish to perform specific actions upon the
  69. destruction of an instance.
  70.  
  71. =back
  72.  
  73. =head1 MORE INFORMATION
  74.  
  75. The L<perltie> section uses a good example of tying scalars by associating
  76. process IDs with priority.
  77.  
  78. =cut
  79.  
  80. use Carp;
  81.  
  82. sub new {
  83.     my $pkg = shift;
  84.     $pkg->TIESCALAR(@_);
  85. }
  86.  
  87.  
  88. sub TIESCALAR {
  89.     my $pkg = shift;
  90.     if (defined &{"{$pkg}::new"}) {
  91.     carp "WARNING: calling ${pkg}->new since ${pkg}->TIESCALAR is missing"
  92.         if $^W;
  93.     $pkg->new(@_);
  94.     }
  95.     else {
  96.     croak "$pkg doesn't define a TIESCALAR method";
  97.     }
  98. }
  99.  
  100. sub FETCH {
  101.     my $pkg = ref $_[0];
  102.     croak "$pkg doesn't define a FETCH method";
  103. }
  104.  
  105. sub STORE {
  106.     my $pkg = ref $_[0];
  107.     croak "$pkg doesn't define a STORE method";
  108. }
  109.  
  110. package Tie::StdScalar;
  111. @ISA = (Tie::Scalar);
  112.  
  113. sub TIESCALAR {
  114.     my $class = shift;
  115.     my $instance = shift || undef;
  116.     return bless \$instance => $class;
  117. }
  118.  
  119. sub FETCH {
  120.     return ${$_[0]};
  121. }
  122.  
  123. sub STORE {
  124.     ${$_[0]} = $_[1];
  125. }
  126.  
  127. sub DESTROY {
  128.     undef ${$_[0]};
  129. }
  130.  
  131. 1;
  132.