http://qs1969.pair.com?node_id=683916


in reply to Re^2: 2*pi*$r -- constant function without prototype
in thread 2*pi*$r -- constant function without prototype

Perl Best Practices recommends Readonly. While constants with use constant are replaced at compile time, Readonly variables are normal variables, just set read-only.
The problem with use constant is that the constants are actual functions which don't interpolate in strings!

I recently needed the line feed ("\012") as constant (Note: "\n" is platform dependent!).
Compare:

use Readonly; Readonly $NL => "\012"; [...] print FILE "header1${NL}header2${NL}"; use constant NL => "\012"; [...] print FILE "header1" . NL . "header2" . NL;
The speed penalty for Readonly is not meaningful in my case. However if you have large calculations which only dependent on constants, maybe inside a loop, then use constant is better because perl can optimise it at compile time.

Replies are listed 'Best First'.
Re^4: 2*pi*$r -- constant function without prototype
by BrowserUk (Patriarch) on May 01, 2008 at 13:53 UTC

    As long as your going to use ${} to interpolate vars into strings, which is generally a good idea. Then you can use constants in a similar way with a single extra '\' character:

    use constant NL => "\012";; print "fred${\NL}bill${\NL}";; fred bill

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      (: Reminds me of creating alias (in a bourne-like shell) to refer to the real command, not another alias (alias ls="\ls -F" ; alias ll="\ls -la").