in reply to What is the difference between the constant construct in Perl and the Readonly construct in Perl?

A constant causes lexical substitution to take place for instances of the bareword found within the scope of the constant declaration.

Readonly controls access to variables not barewords.

The conditions for Readonly instead of using a constant bareword tend to be where the "constant" value is unknown prior to entry into the relevant scope, whereas a constant is known in advance of execution and tends to be used to minimise hardcoding.

# example of constant: # use constant TMPDIR = '/bigdrive/bigbucket01'; use constant TMPDIR = '/tmp'; # /tmp now mapped to above # example of Readonly: sub Write { my $self = shift; Readonly my %reset = %$self; # ... code that modifies $self # e.g. with 'dangerous' use of eval ... $self -> ( STATE } = eval $self -> { COMMAND } or goto ERROR; # ... $self -> Close() and return 1; # if error reset $self: ERROR: delete $self -> { $_} for keys %$self; $self -> { $_ } = $reset{ $_ } for keys %reset; return 0; }

-M

Free your mind

  • Comment on Re: What is the difference between the constant construct in Perl and the Readonly construct in Perl?
  • Download Code