my $const = \"Don't touch me!";
my $num = \5.6;
local *const2 = \"Another string";
local *num2 = \3.14159;
print "[$$const][$$num][$const2][$num2]\n";
### try and modify it
eval{ $num2 = 3; };
print $@;
print "$num2\n";
####
#!/usr/bin/perl -w
use Benchmark qw(timethese cmpthese countit timestr);
use constant CONST_MOD => 3;
sub CONST_SUB () { 3 };
my $const_my = 3;
local $const_local = 3;
my $const_ref = \3;
local *const_alias = \3;
print "mod[".CONST_MOD."]sub[".CONST_SUB."]\n"
."my[$const_my]local[$const_local]\n"
."ref[$$const_ref]alias[$const_alias]\n";
cmpthese (1_000_000,
{
CMod => sub { my $t = CONST_MOD },
CSub => sub { my $t = CONST_SUB },
CMy => sub { my $t = $const_my },
CLocal => sub { my $t = $const_local },
CRef => sub { my $t = $$const_ref },
CAlias => sub { my $t = $const_alias },
});
##
##
mod[3]sub[3]
my[3]local[3]
ref[3]alias[3]
Benchmark: timing 1000000 iterations of CAlias, CLocal, CMod, CMy, CRef, CSub...
CAlias: 0 wallclock secs ( 0.79 usr + 0.00 sys = 0.79 CPU) @ 1265822.78/s (n=1000000)
CLocal: 0 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 1250000.00/s (n=1000000)
CMod: 2 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 1250000.00/s (n=1000000)
CMy: 1 wallclock secs ( 0.75 usr + 0.00 sys = 0.75 CPU) @ 1333333.33/s (n=1000000)
CRef: 0 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 970873.79/s (n=1000000)
CSub: 0 wallclock secs ( 0.81 usr + 0.01 sys = 0.82 CPU) @ 1219512.20/s (n=1000000)
Rate CRef CSub CLocal CMod CAlias CMy
CRef 970874/s -- -20% -22% -22% -23% -27%
CSub 1219512/s 26% -- -2% -2% -4% -9%
CLocal 1250000/s 29% 3% -- -0% -1% -6%
CMod 1250000/s 29% 3% 0% -- -1% -6%
CAlias 1265823/s 30% 4% 1% 1% -- -5%
CMy 1333333/s 37% 9% 7% 7% 5% --