Andrew Main (Zefram)

NAME

Scalar::Construct - build custom kinds of scalar

SYNOPSIS

        use Scalar::Construct qw(constant variable aliasref aliasobj);

        $ref = constant($value);
        $ref = variable($value);
        $ref = aliasref(\$array[0]);
        $ref = aliasobj($array[0]);

DESCRIPTION

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.

FUNCTIONS

Each function has two names. There is a longer descriptive name, and a shorter name to spare screen space and the programmer's fingers.

constant(VALUE)
ro(VALUE)

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.

variable(VALUE)
rw(VALUE)

Creates a fresh writable scalar, initialised to value VALUE, and returns a reference to it.

aliasref(OBJECT_REF)
ar(OBJECT_REF)

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.

aliasobj(OBJECT)
ao(OBJECT)

Returns a reference to OBJECT.

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.

SEE ALSO

Lexical::Var

AUTHOR

Andrew Main (Zefram) <zefram@fysh.org>

COPYRIGHT

Copyright (C) 2012 Andrew Main (Zefram) <zefram@fysh.org>

LICENSE

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.




Hosting generously
sponsored by Bytemark