Scalar::Construct - build custom kinds of scalar
use Scalar::Construct qw(constant variable aliasref aliasobj); $ref = constant($value); $ref = variable($value); $ref = aliasref(\$array[0]); $ref = aliasobj($array[0]);
This module supplies functions to construct Perl scalar objects. While writable (variable) scalars can easily be constructed using the ordinary facilities of the Perl language, immutable (constant) scalars require a library such as this.
Each function has two names. There is a longer descriptive name, and a shorter name to spare screen space and the programmer's fingers.
Creates a fresh immutable scalar, with value VALUE, and returns a reference to it.
If VALUE is actually a compile-time constant that can be expressed as a literal, such as 123, it would appear that a reference to a constant object with that value can be created by a Perl expression such as \123. However, Perl has some bugs relating to compile-time constants that prevent this working as intended. On Perls built for threading (even if threading is not actually used), such a scalar will be copied at surprising times, losing both its object identity and its immutability. The function supplied by this module avoids these problems.
123
\123
Creates a fresh writable scalar, initialised to value VALUE, and returns a reference to it.
OBJECT_REF must be a reference to a scalar. Returns another reference to the same scalar. (This is effectively an identity function, included for completeness.)
Due to the Perl bugs discussed above for "constant", it is unwise to attempt to alias a compile-time constant. Instead use "constant" to create a well-behaved constant scalar.
Returns a reference to OBJECT.
Lexical::Var
Andrew Main (Zefram) <zefram@fysh.org>
Copyright (C) 2012 Andrew Main (Zefram) <zefram@fysh.org>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Scalar::Construct, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Scalar::Construct
CPAN shell
perl -MCPAN -e shell install Scalar::Construct
For more information on module installation, please visit the detailed CPAN module installation guide.