diff options
author | Randy J Ray <rjray@uswest.com> | 1996-02-06 10:04:47 -0700 |
---|---|---|
committer | Andy Dougherty <doughera@lafcol.lafayette.edu> | 1996-02-06 10:04:47 -0700 |
commit | 64d0c9732b520ac8f3a9448f7c3f9f5b4209bd6d (patch) | |
tree | b9565a30f3b8e16afcf8ff1fc652b1b674a0d6c1 /lib/Tie/Scalar.pm | |
parent | 6c31b3367b143518785862fbdca365a32245ff3d (diff) | |
download | perl-64d0c9732b520ac8f3a9448f7c3f9f5b4209bd6d.tar.gz |
Documentation patch: Tie::Hash, Tie::Scalar, Tie::SubstrHash
[This finishes a job started by Randy Ray with perl5.002b1f.]
This patch updates the files lib/TieHash.pm and lib/SubstrHash.pm. Those
two files are moved to a new directory called lib/Tie. A new tie-related
class for scalars, lib/Tie/Scalar.pm, is added. Each of these has pod
documentation that passes through pod2man without complaint. All package
names are updated to reflect this structure, and the "standard" sub-classes
for hashes and scalars are named Tie::StdHash and Tie::StdScalar, in
keeping with the group consensus.
With this, the documentation for Tie::Hash should be fine, Tie::Scalar
exists and is documented, and Tie::SubstrHash (formerly just SubstrHash)
is also documented, albeit weakly. I have no examples of usage to show, and
I suspect a possible bug or two, but without examples it's hard to say.
As it happens, this patch also impacts the DBM-style extensions, as they
(DB_File, NDBM_File, GBM_File, ODBM_File, SDBM_File) all inherited from
the old TieHash.pm as a base class.
Diffstat (limited to 'lib/Tie/Scalar.pm')
-rw-r--r-- | lib/Tie/Scalar.pm | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/lib/Tie/Scalar.pm b/lib/Tie/Scalar.pm new file mode 100644 index 0000000000..2db02ae1da --- /dev/null +++ b/lib/Tie/Scalar.pm @@ -0,0 +1,138 @@ +package Tie::Scalar; + +=head1 NAME + +Tie::Scalar, Tie::StdScalar - base class definitions for tied scalars + +=head1 SYNOPSIS + + package NewScalar; + require Tie::Scalar; + + @ISA = (Tie::Scalar); + + sub FETCH { ... } # Provide a needed method + sub TIESCALAR { ... } # Overrides inherited method + + + package NewStdScalar; + require Tie::Scalar; + + @ISA = (Tie::StdScalar); + + # All methods provided by default, so define only what needs be overridden + sub FETCH { ... } + + + package main; + + tie $new_scalar, NewScalar; + tie $new_std_scalar, NewStdScalar; + +=head1 DESCRIPTION + +This module provides some skeletal methods for scalar-tying classes. See +L<perltie> for a list of the functions required in tying a scalar to a +package. The basic B<Tie::Scalar> package provides a C<new> method, as well +as methods C<TIESCALAR>, C<FETCH> and C<STORE>. The B<Tie::StdScalar> +package provides all the methods specified in L<perltie>. It inherits from +B<Tie::Scalar> and causes scalars tied to it to behave exactly like the +built-in scalars, allowing for selective overloading of methods. The C<new> +method is provided as a means of grandfathering, for classes that forget to +provide their own C<TIESCALAR> method. + +For developers wishing to write their own tied-scalar classes, the methods +are summarized below. The L<perltie> section not only documents these, but +has sample code as well: + +=over + +=item TIESCALAR classname, LIST + +The method invoked by the command C<tie $scalar, classname>. Associates a new +scalar instance with the specified class. C<LIST> would represent additional +arguments (along the lines of L<AnyDBM_File> and compatriots) needed to +complete the association. + +=item FETCH this + +Retrieve the value of the tied scalar referenced by I<this>. + +=item STORE this, value + +Store data I<value> in the tied scalar referenced by I<this>. + +=item DESTROY this + +Free the storage associated with the tied scalar referenced by I<this>. +This is rarely needed, as Perl manages its memory quite well. But the +option exists, should a class wish to perform specific actions upon the +destruction of an instance. + +=back + +=head1 MORE INFORMATION + +The L<perltie> section uses a good example of tying scalars by associating +process IDs with priority. + +=cut + +use Carp; + +sub new { + my $pkg = shift; + $pkg->TIESCALAR(@_); +} + +# "Grandfather" the new, a la Tie::Hash + +sub TIESCALAR { + my $pkg = shift; + if (defined &{"{$pkg}::new"}) { + carp "WARNING: calling ${pkg}->new since ${pkg}->TIESCALAR is missing" + if $^W; + $pkg->new(@_); + } + else { + croak "$pkg doesn't define a TIESCALAR method"; + } +} + +sub FETCH { + my $pkg = ref $_[0]; + croak "$pkg doesn't define a FETCH method"; +} + +sub STORE { + my $pkg = ref $_[0]; + croak "$pkg doesn't define a STORE method"; +} + +# +# The Tie::StdScalar package provides scalars that behave exactly like +# Perl's built-in scalars. Good base to inherit from, if you're only going to +# tweak a small bit. +# +package Tie::StdScalar; +@ISA = (Tie::Scalar); + +sub TIESCALAR { + my $class = shift; + my $instance = shift || undef; + return bless \$instance => $class; +} + +sub FETCH { + return ${$_[0]}; +} + +sub STORE { + ${$_[0]} = $_[1]; +} + +sub DESTROY { + undef ${$_[0]}; +} + +1; |