Problems? Is your data what you think it is? PerlMonks

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

by why_bird (Pilgrim)
 on May 01, 2008 at 10:50 UTC ( #683902=note: print w/replies, xml ) Need Help??

I thought the recommended advice was to use Readonly instead of use constant. Is this wrong, or if not, could you explain the circumstances when you should use each one?

cheers
why_bird
........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......
• Comment on Re^2: 2*pi*\$r -- constant function without prototype

Replies are listed 'Best First'.
Re^3: 2*pi*\$r -- constant function without prototype
by almut (Canon) on May 01, 2008 at 11:21 UTC

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 constant NL => "\012";
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...

The benchmark results vary quite much on my laptop (Perl 5.8.8, Ubuntu). Especially 'constant' and 'direct' are not identical but differ by a factor of 1/2 to 2.

I had the idea to use Data::Dumper with Deparse = 1 on the hash to look how perl optimizes the constants:

```\$VAR1 = {
'direct'    => sub { my \$s = "abc\ndef\nghi"; },
'scalarRW'  => sub { my \$s = "abc\${Nl}def\${Nl}ghi"; },
'constant'  => sub { my \$s = "abc\ndef\nghi"; },
'Readonly'  => sub { my \$s = "abc\${NL}def\${NL}ghi"; },
'constant2' => sub { my \$s = qq[abc\${\"\n";}def\${\"\n";}ghi]
+; },
'ScalarRO'  => sub { my \$s = "abc\${nl}def\${nl}ghi"; }
};
(I removed the use warnings; use strict 'refs'; from each sub to make it easier to read).
As you can see 'constant' is identical with 'direct' after compilation. 'constant2' still has the dereferencing and referencing included, which makes it slow. The others have the variable included and only differ on how it is accessed. Because Readonly is tied it is the slowest.
Re^3: 2*pi*\$r -- constant function without prototype
by BrowserUk (Patriarch) on May 01, 2008 at 11:12 UTC
Re^3: 2*pi*\$r -- constant function without prototype
by mscharrer (Hermit) on May 01, 2008 at 13:16 UTC
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;
[...]

use constant NL => "\012";
[...]
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.

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

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://683902]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2022-11-30 23:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite new Perl feature (in 2022) ...

Results (43 votes). Check out past polls.

Notices?