in reply to There is a way to LOCK a SCALAR?

$ perl -e'$foo=\"I am immutable"; $$foo="fie!";' Modification of a read-only value attempted at -e line 1. $

Is that what you want?

Update: Better yet,

$ perl -e'*foo=\"I am immutable"; $foo="fie!";' Modification of a read-only value attempted at -e line 1. $

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: There is a way to LOCK a SCALAR?
by calin (Deacon) on Dec 19, 2003 at 12:35 UTC
    And for lexical variables, you can use the Lexical::Alias module:

    $ perl use Lexical::Alias 'alias_r'; my $s; alias_r \"I am immutable", \$s; $s = "fie!"; ^D Modification of a read-only value attempted at - line 6. $

    Update: I don't think it's documented behaviour. It just happens to work, probably because of the way the module is implemented (I am by no means a perlguts expert - and I haven't studied the source). Here's a snippet from the module documentation:

    alias_r(\src, \dst)

    Makes dst (which must be lexical) an alias to src (which can be either lexical or a package variable). src and dst must be the same data type (scalar and scalar, array and array, hash and hash). This is not exported by default.

    Of course you can play by the rules:

    [...] my $s; { local $tmp; *tmp=\"I am immutable"; alias_r \$tmp, \$s; } [...]