in reply to Re: There is a way to LOCK a SCALAR?
in thread There is a way to LOCK a SCALAR?

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".

Replies are listed 'Best First'.
Re: Re: Re: There is a way to LOCK a SCALAR?
by BUU (Prior) on Dec 18, 2003 at 21:20 UTC
    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".

        You can always add

        sub UNTIE { die "Don't do that!" }

        to the class too. :-)

        I think my point that in this case education would be better then chains. As in, tell your users/developers why you shouldn't mess with this scalar instead of trying to devise elaborate hacks to not let them.