1Tie::Scalar(3pm) Perl Programmers Reference Guide Tie::Scalar(3pm)
2
3
4
6 Tie::Scalar, Tie::StdScalar - base class definitions for tied scalars
7
9 package NewScalar;
10 require Tie::Scalar;
11
12 @ISA = qw(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 = qw(Tie::StdScalar);
22
23 # All methods provided by default, so define only what needs be overridden
24 sub FETCH { ... }
25
26
27 package main;
28
29 tie $new_scalar, 'NewScalar';
30 tie $new_std_scalar, 'NewStdScalar';
31
33 This module provides some skeletal methods for scalar-tying classes.
34 See perltie for a list of the functions required in tying a scalar to a
35 package. The basic Tie::Scalar package provides a "new" method, as well
36 as methods "TIESCALAR", "FETCH" and "STORE". The Tie::StdScalar package
37 provides all the methods specified in perltie. It inherits from
38 Tie::Scalar and causes scalars tied to it to behave exactly like the
39 built-in scalars, allowing for selective overloading of methods. The
40 "new" method is provided as a means of grandfathering, for classes that
41 forget to provide their own "TIESCALAR" method.
42
43 For developers wishing to write their own tied-scalar classes, the
44 methods are summarized below. The perltie section not only documents
45 these, but has sample code as well:
46
47 TIESCALAR classname, LIST
48 The method invoked by the command "tie $scalar, classname".
49 Associates a new scalar instance with the specified class. "LIST"
50 would represent additional arguments (along the lines of
51 AnyDBM_File and compatriots) needed to complete the association.
52
53 FETCH this
54 Retrieve the value of the tied scalar referenced by this.
55
56 STORE this, value
57 Store data value in the tied scalar referenced by this.
58
59 DESTROY this
60 Free the storage associated with the tied scalar referenced by
61 this. This is rarely needed, as Perl manages its memory quite
62 well. But the option exists, should a class wish to perform
63 specific actions upon the destruction of an instance.
64
65 Tie::Scalar vs Tie::StdScalar
66 "Tie::Scalar" provides all the necessary methods, but one should
67 realize they do not do anything useful. Calling "Tie::Scalar::FETCH" or
68 "Tie::Scalar::STORE" results in a (trappable) croak. And if you inherit
69 from "Tie::Scalar", you must provide either a "new" or a "TIESCALAR"
70 method.
71
72 If you are looking for a class that does everything for you you don't
73 define yourself, use the "Tie::StdScalar" class, not the "Tie::Scalar"
74 one.
75
77 The perltie section uses a good example of tying scalars by associating
78 process IDs with priority.
79
80
81
82perl v5.16.3 2013-02-26 Tie::Scalar(3pm)