coldhawk has asked for the wisdom of the Perl Monks concerning the following question:

i was wondering that if it's possible to example create a hash in some block and set it read only,
so when we get out of that block you could not modify the hash at any way,
but of course we need to be able to read it still.

Any thoughts about this?

Replies are listed 'Best First'.
Re: read only variable
by NetWallah (Canon) on May 04, 2004 at 23:14 UTC
    How about use ReadOnly (Formerly known as 'Constant' with a capital "C").?
    Readonly::Hash %has => (key => value, key => value, ...);

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.
Re: read only variable
by sgifford (Prior) on May 04, 2004 at 23:51 UTC
    You can emulate that with closures, although you'll be working with a subroutine instead of a hash:
    sub make_readonly { my %h = @_; sub { $h{$_[0]}; } } my $v = make_readonly(name => 'Scott', shoesize => '9.5'); print $v->('name'),"'s shoes are size ",$v->('shoesize'),"\n";
Re: read only variable
by mifflin (Curate) on May 04, 2004 at 23:08 UTC
    The only thing that comes to mind is Tie::Hash. The Store method could be set to not do any thing or even call warn (or carp/cluck if you use Carp)
Re: read only variable
by bart (Canon) on May 05, 2004 at 21:57 UTC
    Look into Hash::Util. Plenty of routines to choose from, lock_hash() is the most brutal one.