Something that will let you use scalars but will prevent them being modified:
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";
The first ones are not as pretty as dollar variables, and the second have a slight overhead of local, but I think that should give you some nice unmodifiable variables.
UPDATE:
Benchmarks are always good. Read em and weep:
#!/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 },
});
Produces the following:
mod[3]sub[3]
my[3]local[3]
ref[3]alias[3]
Benchmark: timing 1000000 iterations of CAlias, CLocal, CMod, CMy, CRe
+f, CSub...
CAlias: 0 wallclock secs ( 0.79 usr + 0.00 sys = 0.79 CPU) @ 12
+65822.78/s (n=1000000)
CLocal: 0 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 12
+50000.00/s (n=1000000)
CMod: 2 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 12
+50000.00/s (n=1000000)
CMy: 1 wallclock secs ( 0.75 usr + 0.00 sys = 0.75 CPU) @ 13
+33333.33/s (n=1000000)
CRef: 0 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 97
+0873.79/s (n=1000000)
CSub: 0 wallclock secs ( 0.81 usr + 0.01 sys = 0.82 CPU) @ 12
+19512.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% --
The only noticable differences are the my and the ref. The reference takes too long, the "my" is a little quicker but it is modifiable. All of the others are neck a neck, so, I'd vote for
local *var=\23;
my @a=qw(random brilliant braindead); print $a[rand(@a)];