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