1Scalar::Construct(3) User Contributed Perl Documentation Scalar::Construct(3)
2
3
4
6 Scalar::Construct - build custom kinds of scalar
7
9 use Scalar::Construct qw(constant variable aliasref aliasobj);
10
11 $ref = constant($value);
12 $ref = variable($value);
13 $ref = aliasref(\$array[0]);
14 $ref = aliasobj($array[0]);
15
17 This module supplies functions to construct Perl scalar objects. While
18 writable (variable) scalars can easily be constructed using the
19 ordinary facilities of the Perl language, immutable (constant) scalars
20 require a library such as this.
21
23 Each function has two names. There is a longer descriptive name, and a
24 shorter name to spare screen space and the programmer's fingers.
25
26 constant(VALUE)
27 ro(VALUE)
28 Creates a fresh immutable scalar, with value VALUE, and returns a
29 reference to it.
30
31 If VALUE is actually a compile-time constant that can be expressed
32 as a literal, such as 123, it would appear that a reference to a
33 constant object with that value can be created by a Perl expression
34 such as "\123". However, Perl has some bugs relating to compile-
35 time constants that prevent this working as intended. On Perls
36 built for threading (even if threading is not actually used), such
37 a scalar will be copied at surprising times, losing both its object
38 identity and its immutability. The function supplied by this
39 module avoids these problems.
40
41 variable(VALUE)
42 rw(VALUE)
43 Creates a fresh writable scalar, initialised to value VALUE, and
44 returns a reference to it.
45
46 aliasref(OBJECT_REF)
47 ar(OBJECT_REF)
48 OBJECT_REF must be a reference to a scalar. Returns another
49 reference to the same scalar. (This is effectively an identity
50 function, included for completeness.)
51
52 Due to the Perl bugs discussed above for "constant", it is unwise
53 to attempt to alias a compile-time constant. Instead use
54 "constant" to create a well-behaved constant scalar.
55
56 aliasobj(OBJECT)
57 ao(OBJECT)
58 Returns a reference to OBJECT.
59
60 Due to the Perl bugs discussed above for "constant", it is unwise
61 to attempt to alias a compile-time constant. Instead use
62 "constant" to create a well-behaved constant scalar.
63
65 Lexical::Var
66
68 Andrew Main (Zefram) <zefram@fysh.org>
69
71 Copyright (C) 2012 Andrew Main (Zefram) <zefram@fysh.org>
72
74 This module is free software; you can redistribute it and/or modify it
75 under the same terms as Perl itself.
76
77
78
79perl v5.36.0 2023-01-20 Scalar::Construct(3)