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

A Better reason that I find for using Readonly is that is makes your code easier to understand. If you do not have Readonly with XS compiled then you take a performance hit but you generally only see that using a Readonly hash.

Another Advantage Readonly has over use constant is that you can declare a hash, you cannot do that with use constant.

Replies are listed 'Best First'.
Re^4: Using Constants in Perl
by the_slycer (Chaplain) on Mar 01, 2006 at 01:15 UTC
    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() }) { ... }

      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;