La función readonly en Scalar::Util permite determinar si un argumento es una variable o un valor:
lhp@nereida:~/Lperl/src/testing$ cat -n readonly.pl 1 use warnings; 2 use strict; 3 use Carp; 4 use Scalar::Util qw(readonly); 5 6 sub ro { 7 print "readonly($_[0])=".readonly($_[0])."\n"; 8 $_[0] = 'xxx'; 9 } 10 11 my $x = 4; 12 13 eval { ro($x) } or print $@; 14 eval { ro(-8) } or print $@; 15 eval { ro('lonely') } or print $@;Cuando se ejecuta, el programa anterior produce una salida como:
lhp@nereida:~/Lperl/src/testing$ perl readonly.pl readonly(4)=0 readonly(-8)=8388608 Modification of a read-only value attempted at readonly.pl line 8. readonly(lonely)=8388608 Modification of a read-only value attempted at readonly.pl line 8.
Casiano Rodríguez León