Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Can I take the address of a Perl variable?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Larry Wall  
View profile  
 More options May 28 1992, 2:30 pm
Newsgroups: comp.lang.perl, alt.hackers
Followup-To: comp.lang.perl
From: lw...@netlabs.com (Larry Wall)
Date: 28 May 92 22:59:35 GMT
Local: Thurs, May 28 1992 6:59 pm
Subject: Re: Can I take the address of a Perl variable?
In article <rantapaa.706981...@s6.math.umn.edu> ranta...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.