Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I take the address of a Perl variable?

69 views
Skip to first unread message

Larry Wall

unread,
May 28, 1992, 6:59:35 PM5/28/92
to
In article <rantapaa....@s6.math.umn.edu> rant...@s6.math.umn.edu (Erik E. Rantapaa) writes:
: Is there ANY possible way of taking the address of Perl variable?
: Yes, I know that the Perl manual states that there is no address-of
: operator like C has and that such an operator goes against the
: entire philosophy of Perl, BUT inquiring minds want to know.

That's what the "p" template spec for pack does, just so happens...

: Also, if it were possible to determine the address of a variable,
: would that address be safe for asynchronous I/O? I.e. is it possible
: for the address of a Perl variable to change if it is never modified
: by the Perl program?

In general, it's safe. The address of a string will stay the same as long
as you don't change its length. (With some mallocs, it's actually a bit
more flexible, but that's gamblin'...)

: While on this the train of thought, I suppose I should ask the dual
: question to the first, namely is it possible to make arbitrary memory
: references in Perl? You never know when &peek and &poke might come in
: handy :^)
:
: I realize that all of the above can be done in a customized version
: of Perl -- I was wondering if any of them could be accomplished using
: some tricky features (bugs?) of vanilla Perl.
:
: Also please note that this is NOT a feature request. I merely want to
: know if it is possible.

I'm tempted to say "Yes, it's possible," and leave it at that. However,
in the interests of free speech and all, here's a bit of code the
peeks or pokes an arbitrary four-byte value. Season to taste.

#!/usr/bin/perl

# The following block would ordinarily be "required".
{
package peek;

$str_tmpl = "LLdLLcccc";

$fakestr = pack($str_tmpl, 0, 4, 0.0, 4, 0, 1, 0, 0, 0);
$fakeaddr = unpack("L", pack("p", $fakestr));

substr(*fakestab, 4, 4) = pack("L", $fakeaddr);

sub main'peek {
substr($fakestr,0,4) = pack("L", $_[0]);
eval '$fakestab';
}

sub main'poke {
substr($fakestr,0,4) = pack("L", $_[0]);
eval 'substr($fakestab,0,4) = $_[1]';
}
}

$targ = "hi there";

$addr = unpack("L", pack("p", $targ));
printf "%lx\n", $addr;

print &peek($addr),"\n";
&poke($addr, "lo t");
print $targ,"\n";

It would probably want some bulletproofing for any real use, if there
actually exists any legitimate real use... :-)

But for amazing your friends and bewildering your enemies, it might come
in pretty handy.

Mind you, there's absolutely no guarantee that this will work at any
arbitrary time in the future. It's not even portable to machines that
have char* pointers that are different in size from longs.

Larry

0 new messages