7 Tie::Scalar, Tie::StdScalar - base class definitions for tied scalars
14 @ISA = qw(Tie::Scalar);
16 sub FETCH { ... } # Provide a needed method
17 sub TIESCALAR { ... } # Overrides inherited method
23 @ISA = qw(Tie::StdScalar);
25 # All methods provided by default, so define
26 # only what needs be overridden
32 tie $new_scalar, 'NewScalar';
33 tie $new_std_scalar, 'NewStdScalar';
37 This module provides some skeletal methods for scalar-tying classes. See
38 L<perltie> for a list of the functions required in tying a scalar to a
39 package. The basic B<Tie::Scalar> package provides a C<new> method, as well
40 as methods C<TIESCALAR>, C<FETCH> and C<STORE>. The B<Tie::StdScalar>
41 package provides all the methods specified in L<perltie>. It inherits from
42 B<Tie::Scalar> and causes scalars tied to it to behave exactly like the
43 built-in scalars, allowing for selective overloading of methods. The C<new>
44 method is provided as a means of grandfathering, for classes that forget to
45 provide their own C<TIESCALAR> method.
47 For developers wishing to write their own tied-scalar classes, the methods
48 are summarized below. The L<perltie> section not only documents these, but
49 has sample code as well:
53 =item TIESCALAR classname, LIST
55 The method invoked by the command C<tie $scalar, classname>. Associates a new
56 scalar instance with the specified class. C<LIST> would represent additional
57 arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
58 complete the association.
62 Retrieve the value of the tied scalar referenced by I<this>.
64 =item STORE this, value
66 Store data I<value> in the tied scalar referenced by I<this>.
70 Free the storage associated with the tied scalar referenced by I<this>.
71 This is rarely needed, as Perl manages its memory quite well. But the
72 option exists, should a class wish to perform specific actions upon the
73 destruction of an instance.
77 =head2 Tie::Scalar vs Tie::StdScalar
79 C<< Tie::Scalar >> provides all the necessary methods, but one should realize
80 they do not do anything useful. Calling C<< Tie::Scalar::FETCH >> or
81 C<< Tie::Scalar::STORE >> results in a (trappable) croak. And if you inherit
82 from C<< Tie::Scalar >>, you I<must> provide either a C<< new >> or a
83 C<< TIESCALAR >> method.
85 If you are looking for a class that does everything for you you don't
86 define yourself, use the C<< Tie::StdScalar >> class, not the
87 C<< Tie::Scalar >> one.
89 =head1 MORE INFORMATION
91 The L<perltie> section uses a good example of tying scalars by associating
92 process IDs with priority.
97 use warnings::register;
104 # "Grandfather" the new, a la Tie::Hash
108 my $pkg_new = $pkg -> can ('new');
110 if ($pkg_new and $pkg ne __PACKAGE__) {
111 my $my_new = __PACKAGE__ -> can ('new');
112 if ($pkg_new == $my_new) {
116 croak "$pkg must define either a TIESCALAR() or a new() method";
119 warnings::warnif ("WARNING: calling ${pkg}->new since " .
120 "${pkg}->TIESCALAR is missing");
124 croak "$pkg doesn't define a TIESCALAR method";
130 croak "$pkg doesn't define a FETCH method";
135 croak "$pkg doesn't define a STORE method";
139 # The Tie::StdScalar package provides scalars that behave exactly like
140 # Perl's built-in scalars. Good base to inherit from, if you're only going to
143 package Tie::StdScalar;
144 @ISA = qw(Tie::Scalar);
148 my $instance = @_ ? shift : undef;
149 return bless \$instance => $class;