xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Tie/Scalar.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage Tie::Scalar;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateour $VERSION = '1.00';
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate=head1 NAME
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateTie::Scalar, Tie::StdScalar - base class definitions for tied scalars
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate=head1 SYNOPSIS
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate    package NewScalar;
12*0Sstevel@tonic-gate    require Tie::Scalar;
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate    @ISA = (Tie::Scalar);
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gate    sub FETCH { ... }		# Provide a needed method
17*0Sstevel@tonic-gate    sub TIESCALAR { ... }	# Overrides inherited method
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate    package NewStdScalar;
21*0Sstevel@tonic-gate    require Tie::Scalar;
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate    @ISA = (Tie::StdScalar);
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate    # All methods provided by default, so define only what needs be overridden
26*0Sstevel@tonic-gate    sub FETCH { ... }
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate    package main;
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate    tie $new_scalar, 'NewScalar';
32*0Sstevel@tonic-gate    tie $new_std_scalar, 'NewStdScalar';
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate=head1 DESCRIPTION
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gateThis module provides some skeletal methods for scalar-tying classes. See
37*0Sstevel@tonic-gateL<perltie> for a list of the functions required in tying a scalar to a
38*0Sstevel@tonic-gatepackage. The basic B<Tie::Scalar> package provides a C<new> method, as well
39*0Sstevel@tonic-gateas methods C<TIESCALAR>, C<FETCH> and C<STORE>. The B<Tie::StdScalar>
40*0Sstevel@tonic-gatepackage provides all the methods specified in  L<perltie>. It inherits from
41*0Sstevel@tonic-gateB<Tie::Scalar> and causes scalars tied to it to behave exactly like the
42*0Sstevel@tonic-gatebuilt-in scalars, allowing for selective overloading of methods. The C<new>
43*0Sstevel@tonic-gatemethod is provided as a means of grandfathering, for classes that forget to
44*0Sstevel@tonic-gateprovide their own C<TIESCALAR> method.
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gateFor developers wishing to write their own tied-scalar classes, the methods
47*0Sstevel@tonic-gateare summarized below. The L<perltie> section not only documents these, but
48*0Sstevel@tonic-gatehas sample code as well:
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate=over 4
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate=item TIESCALAR classname, LIST
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gateThe method invoked by the command C<tie $scalar, classname>. Associates a new
55*0Sstevel@tonic-gatescalar instance with the specified class. C<LIST> would represent additional
56*0Sstevel@tonic-gatearguments (along the lines of L<AnyDBM_File> and compatriots) needed to
57*0Sstevel@tonic-gatecomplete the association.
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate=item FETCH this
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gateRetrieve the value of the tied scalar referenced by I<this>.
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate=item STORE this, value
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gateStore data I<value> in the tied scalar referenced by I<this>.
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate=item DESTROY this
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gateFree the storage associated with the tied scalar referenced by I<this>.
70*0Sstevel@tonic-gateThis is rarely needed, as Perl manages its memory quite well. But the
71*0Sstevel@tonic-gateoption exists, should a class wish to perform specific actions upon the
72*0Sstevel@tonic-gatedestruction of an instance.
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate=back
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate=head1 MORE INFORMATION
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gateThe L<perltie> section uses a good example of tying scalars by associating
79*0Sstevel@tonic-gateprocess IDs with priority.
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate=cut
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gateuse Carp;
84*0Sstevel@tonic-gateuse warnings::register;
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gatesub new {
87*0Sstevel@tonic-gate    my $pkg = shift;
88*0Sstevel@tonic-gate    $pkg->TIESCALAR(@_);
89*0Sstevel@tonic-gate}
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate# "Grandfather" the new, a la Tie::Hash
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gatesub TIESCALAR {
94*0Sstevel@tonic-gate    my $pkg = shift;
95*0Sstevel@tonic-gate	if ($pkg->can('new') and $pkg ne __PACKAGE__) {
96*0Sstevel@tonic-gate	warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIESCALAR is missing");
97*0Sstevel@tonic-gate	$pkg->new(@_);
98*0Sstevel@tonic-gate    }
99*0Sstevel@tonic-gate    else {
100*0Sstevel@tonic-gate	croak "$pkg doesn't define a TIESCALAR method";
101*0Sstevel@tonic-gate    }
102*0Sstevel@tonic-gate}
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gatesub FETCH {
105*0Sstevel@tonic-gate    my $pkg = ref $_[0];
106*0Sstevel@tonic-gate    croak "$pkg doesn't define a FETCH method";
107*0Sstevel@tonic-gate}
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gatesub STORE {
110*0Sstevel@tonic-gate    my $pkg = ref $_[0];
111*0Sstevel@tonic-gate    croak "$pkg doesn't define a STORE method";
112*0Sstevel@tonic-gate}
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate#
115*0Sstevel@tonic-gate# The Tie::StdScalar package provides scalars that behave exactly like
116*0Sstevel@tonic-gate# Perl's built-in scalars. Good base to inherit from, if you're only going to
117*0Sstevel@tonic-gate# tweak a small bit.
118*0Sstevel@tonic-gate#
119*0Sstevel@tonic-gatepackage Tie::StdScalar;
120*0Sstevel@tonic-gate@ISA = (Tie::Scalar);
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gatesub TIESCALAR {
123*0Sstevel@tonic-gate    my $class = shift;
124*0Sstevel@tonic-gate    my $instance = shift || undef;
125*0Sstevel@tonic-gate    return bless \$instance => $class;
126*0Sstevel@tonic-gate}
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gatesub FETCH {
129*0Sstevel@tonic-gate    return ${$_[0]};
130*0Sstevel@tonic-gate}
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gatesub STORE {
133*0Sstevel@tonic-gate    ${$_[0]} = $_[1];
134*0Sstevel@tonic-gate}
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gatesub DESTROY {
137*0Sstevel@tonic-gate    undef ${$_[0]};
138*0Sstevel@tonic-gate}
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate1;
141