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