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

The user? The user doesn't use Perl, the developer uses Perl.

Why do you want a scalar and not a sub?

Anyway, others have already suggested assigning a reference to a literal (a literal is a readonly scalar variable, really!) to a typeglob, but there is a way to turn the readonly flag on during runtime, using xmath's Spy module, which unfortunately he never finished.

use Spy; my $foo = 123; spy(\$foo)->readonly = 1; $foo++; # Modification of a read-only value attempted at - line 5.
Note that with Spy you can do evil things. Some variables are supposed to always be read-only. Like undef (${\undef}), true (${\!0}) and false (${\!1}). It's fun to make them writable, though, so feel free to experiment.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Replies are listed 'Best First'.
Re: Re: There is a way to LOCK a SCALAR?
by gmpassos (Priest) on Dec 18, 2003 at 20:40 UTC
    I just have looked the module Spy, and the author have implemented the module B::More, that can set the readonly flag.! Thanks for the tip.

    But I saw that already exists a module for that, Internals, that let you handle this things. Thanks monks!

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

Re: Re: There is a way to LOCK a SCALAR?
by gmpassos (Priest) on Dec 18, 2003 at 20:24 UTC
    > The user? The user doesn't use Perl, the developer uses Perl.

    Duh! How a bout another Perl developer using the enverioment that I'm developing?!

    Well, I say that because I work building softwares for developers, not for end users.

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

      Duh! How a bout another Perl developer using the enverioment that I'm developing?! Well, I say that because I work building softwares for developers, not for end users.

      It is very un-perlish to lock something. You don't know what creative solutions others invent using your code, and I think it's better to free your code and let others use your code much the way you use Perl. Locking a scalar makes no sense. It's probably a sign that you need another "user" interface (API). Maybe you want OO with a protected property. Perhaps you just need a simple function that returns a value. But I seriously doubt you really need a scalar that is readonly.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        This is just to avoid the user to undef the internal object. This really won't crash all the enverioment, mas it's script won't work until the end. But the enverioment will be still active to can load future scripts.

        Well, but if the user really wan't to reset the SCALAR it always can, it just need to make some XS and load it, than it can do everything!

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