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.

138 lines
3.2 KiB

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