2

I believe my title may be a bit confusing, but here is the gist of what I am trying to do. I know this isn't "the right way" to do this, I'm more just curious if it is possible.

So lets say I have the script:

#!/usr/bin/perl

use strict;
use warnings;

my $foo = "bar";
my $foo_location = int(\$foo);

print $foo_location;

The output of this will be something like, 136167064

How can I take that integer (the location of $foo in memory), and create another variable which is a reference to $foo, using that integer.

So what I'd want to do is something like:

my $foo_reference = some_magical_function_call($foo_location);

Which would allow modifying the dereference of $foo_reference, to modify the value of $foo. So after whatever magic allows this to work,

$$foo_reference eq $foo

Would evaluate to true

I want to say that I figured this out a couple of years ago, but maybe I'm crazy.

Caterham
  • 2,301
  • 1
  • 13
  • 8

1 Answers1

4

Ah, yikes. I found the answer. I probably should have waited before posting this.

Anyway, if anyone is curious:

#!/usr/bin/perl
use strict;
use warnings;
use B;


my $foo = "bar";
my $foo_location = int(\$foo);

my $real_ref = bless(\(0+hex sprintf("%0x",$foo_location)), "B::AV")->object_2svref;

print $$real_ref;

Prints bar

I figured this out from the answer to this question

Community
  • 1
  • 1
Caterham
  • 2,301
  • 1
  • 13
  • 8
  • 1
    you could just do `bless(\$foo_location, "B::AV")->object_2svref` since you already have the int value. – ysth Mar 28 '11 at 07:20