Not sure if I would recommend Readonly over constant —
at least some people have been bitten by its subtle peculiarities. As an example,
see this thread.
BTW, there's also Scalar::Readonly, which is directly manipulating
the readonly-flag of scalars (instead of working with tied magic)...
___
Update: (...it was only a matter of time until someone would post a benchmark, so... :)
use strict;
use warnings;
use Benchmark 'cmpthese';
use Readonly;
use Scalar::Readonly ':all';
use constant NL => "\012";
Readonly my $NL => "\012";
my $nl = "\012"; readonly_on($nl);
my $Nl = "\012";
cmpthese 2000000 => {
constant => sub { my $s = "abc".NL."def".NL."ghi"; },
constant2 => sub { my $s = "abc${\NL}def${\NL}ghi"; },
Readonly => sub { my $s = "abc${NL}def${NL}ghi"; },
ScalarRO => sub { my $s = "abc${nl}def${nl}ghi"; },
scalarRW => sub { my $s = "abc${Nl}def${Nl}ghi"; },
direct => sub { my $s = "abc\012def\012ghi"; },
};
__END__
Rate Readonly constant2 scalarRW ScalarRO constant
+ direct
Readonly 77851/s -- -84% -85% -85% -96%
+ -96%
constant2 484262/s 522% -- -5% -5% -75%
+ -75%
scalarRW 510204/s 555% 5% -- -0% -74%
+ -74%
ScalarRO 511509/s 557% 6% 0% -- -74%
+ -74%
constant 1960784/s 2419% 305% 284% 283% --
+ -0%
direct 1960784/s 2419% 305% 284% 283% 0%
+ --
(as running the benchmark multiple times suggests, differences up to around 5% are insignificant)
___
BTW, I think I remember PBP somewhere saying it's not meant to be treated as a bible...
-
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.