Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: 2*pi*$r -- constant function without prototype

by mscharrer (Hermit)
on May 01, 2008 at 13:16 UTC ( #683916=note: print w/replies, xml ) Need Help??


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").

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2022-12-01 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?