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

Did you check CPAN for a module? If there isn't one, you could write one yourself to use the tie mechanism to subvert changing the value of the scalar. perldoc perltie

Here's a quick implementation (untested):

#!/usr/bin/perl package ConstScalar; sub TIESCALAR { my $class = shift; my $val = shift; return bless \$val, $class } sub STORE { } sub FETCH { my $self = shift; return $$self; } package main; my $pi; tie $pi, 'ConstScalar', 3.14159265358979323846; print "$pi\n"; $pi = 5; # no effect print "$pi\n";

Replies are listed 'Best First'.
Re: Re: There is a way to LOCK a SCALAR?
by gmpassos (Priest) on Dec 18, 2003 at 20:20 UTC
    But the user still can do an UNTIE!
    package main; my $pi; tie $pi, 'ConstScalar', 3.14159265358979323846; print "$pi\n"; untie $pi ; #### NOW AS A NORMAL SCALAR $pi = 5; # effect! print "$pi\n";
    But thanks for the idea! ;-P

    Graciliano M. P.
    "Creativity is the expression of the liberty".

      This is perl mate, no matter what you do, it is extremely likely that anyone else writing code that uses it will be able to basically anything they want to it. Theres no harm in trying to create some guidelines and trying to enforce them at a basic level, but if they don't want to deal with them they can find a way around them. Think of it like 'use strict', its mostly helpful to catch mistakes but if you really need to do something it won't allow you can turn it off.
        Yes you are right!

        I found a module to set the readonly flag in the scalar:

        Internals::SetReadOnly(\$foo);
        But the user still can use the same module to unset the flag! ;-P

        Soo, using tie or XS to make this will be the same! Maybe I will use tie, since it's pure Perl and I'm already using a lot of tie in the enverioment for other things.

        Graciliano M. P.
        "Creativity is the expression of the liberty".