On Tue, Aug 13, 2002 at 03:06:40PM -0400, Deven T. Corzine wrote:
> The only accurate way to know if the code modifies the variables is to do
> some sort of dataflow analysis, and it can't be 100% accurate even then.
> (Suppose a "shift" may or may not happen, depending on the parameters, then
> $_[2] is modified?) Of course, it should often be possible (at least in
> principle) to determine that it's impossible for a particular parameter to
> be modified, and play it safe by assuming "is rw" for undeterminable cases.
Well, perl5 does already manage to avoid auto-vivifying hash keys when they
are used as subroutine arguments. It uses magic, rather than dataflow
analysis:
$ cat autovivi.pl
#!/usr/local/bin/perl -w
use strict;
use Devel::Peek;
sub rval {
my $a = $_[0];
}
sub lval {
$_[0] = undef;
}
sub dumpit {
Dump ($_[0]);
}
my %a = (camel => 2);
rval ($a{llama});
print "rval: @{[keys %a]}\n";
lval ($a{alpaca});
print "lval: @{[keys %a]}\n\n";
dumpit ($a{dromedary});
__END__
$ ./autovivi.pl
rval: camel
lval: camel alpaca
SV = PVLV(0x1134d8) at 0xf4368
REFCNT = 1
FLAGS = (GMG,SMG)
IV = 0
NV = 0
PV = 0
MAGIC = 0x113420
MG_VIRTUAL = &PL_vtbl_defelem
MG_TYPE = PERL_MAGIC_defelem(y)
MG_FLAGS = 0x02
REFCOUNTED
MG_OBJ = 0xf43a4
SV = PVIV(0xf4990) at 0xf43a4
REFCNT = 1
FLAGS = (POK,pPOK)
IV = 0
PV = 0x1138a0 "dromedary"\0
CUR = 9
LEN = 10
TYPE = y
TARGOFF = 0
TARGLEN = 1
TARG = 0x1329ec
SV = PVHV(0x114820) at 0x1329ec
REFCNT = 2
FLAGS = (PADBUSY,PADMY,SHAREKEYS)
IV = 2
NV = 0
ARRAY = 0x1133f8 (0:6, 1:2)
hash quality = 125.0%
KEYS = 2
FILL = 2
MAX = 7
RITER = -1
EITER = 0x0
Elt "camel" HASH = 0x787f96b8
SV = IV(0x112ee4) at 0xf4284
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 2
Elt "alpaca" HASH = 0x5af8b041
SV = PVNV(0x12a6d8) at 0x111b40
REFCNT = 1
FLAGS = ()
IV = 0
NV = 0
PV = 0
As you can see, the ingredients for "magic" are quite cumbersome, but do get
the job done.
Nicholas Clark
--
Even better than the real thing: http://nms-cgi.sourceforge.net/