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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|