Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: Using Constants in Perl

by the_slycer (Chaplain)
on Mar 01, 2006 at 01:15 UTC ( [id://533553]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Using Constants in Perl
in thread Using Constants in Perl

I was pretty sure you could declare a hash with use constant.. or well, a hash reference anyways:
use constant HASH_REF => { constant_key => 'constant_value', ... };
Then use it like you would any hashref ie:
print HASH_REF->{constant_key}
or
foreach my $key (keys %{ &HASH_REF }) { ... }
or the even weirder:
foreach my $key (keys %{ HASH_REF() }) { ... }

Replies are listed 'Best First'.
Re^5: Using Constants in Perl
by jhourcle (Prior) on Mar 01, 2006 at 14:22 UTC

    You can declare it, yes, but it's not read only. The reference itself remains constant, but the data that it's referring to can be changed:

    use constant HASH_REF => { key => 'value' }; HASH_REF->{new_key} = 'new value'; HASH_REF->{key} = 'modified'; use Data::Dumper; print Dumper HASH_REF;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://533553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found