http://www.perlmonks.org?node_id=406446


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/