in reply to Re: Top 10 things to read when your flight is canceled
in thread Top 10 things to read when your flight is canceled

In Perl Best Practices (Ch. 4) TheDamian recommends "use Readonly" instead of "use constant", both because it prevents further modification of the value, and because Readonly gives your constants the syntax properties you would expect to go with their having a sigil.
  • Comment on Re^2: Top 10 things to read when your flight is canceled

Replies are listed 'Best First'.
Re^3: Top 10 things to read when your flight is canceled
by bart (Canon) on Mar 14, 2007 at 11:36 UTC
    The disadvantage to Readonly is that it isn't optimized away, if it was possible. For example:
    use constant DEBUG => 0; print STDERR "foo = $foo\n" if DEBUG;
    The second statement gets optimized away. But in
    use Readonly; Readonly::Scalar $DEBUG => 0; print STDERR "foo = $foo\n" if $DEBUG;
    the last statement is not optimized away, and the test is indeed performed every time the statement is encountered.

    Even worse: a Readonly variable is a tied variable, so the test will be slower than it would have been if it wasn't Readonly.