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


In reply to Re^3: 2*pi*$r -- constant function without prototype by almut
in thread 2*pi*$r -- constant function without prototype by ambrus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.