Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to get the variable or function address in memory?

by rcaputo (Chaplain)
on Nov 09, 2004 at 19:20 UTC ( #406446=note: print w/replies, xml ) Need Help??


in reply to How to get the variable or function address in memory?

The memory address is the numeric version of the reference. Some sample code follows. As an added bonus, it illustrates the use of the mysterious "P" unpack() template.

#!/usr/bin/perl -wl use strict; my $target = 'IHBT'; print "target contains : ``$target''"; # Numify the reference, and derive a pointer from it. my $target_address = \$target + 0; my $native_version = pack 'L', $target_address; my $pointer = pack 'P', $native_version; print "target address : ", sprintf('%x', $target_address); # Walk Perl's variable structures. # This code depends on Perl's current implementation. I advise # against using it in production code. my $sv = unpack('P4', $native_version); print "deref'd packed (sv) : ", sprintf('%x', unpack('L', $sv)); my $pv = unpack('P4', $sv); print "deref'd sv (pv) : ", sprintf('%x', unpack('L', $pv)); my $value = unpack('P' . length($target), $pv); print "deref'd pv (value) : ``$value''\n";

-- Rocco Caputo - http://poe.perl.org/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://406446]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2022-06-30 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?