tags:

views:

271

answers:

4

Is there any way to get Perl to convert the stringified version e.g (ARRAY(0x8152c28)) of an array reference to the actual array reference?

For example

perl -e 'use Data::Dumper; $a = [1,2,3];$b = $a; $a = $a.""; warn Dumper (Then some magic happens);'

would yield

$VAR1 = [
      1,
      2,
      3
    ];
+5  A: 

The first question is: do you really want to do this?

Where is that string coming from?

If it's coming from outside your Perl program, the pointer value (the hex digits) are going to be meaningless, and there's no way to do it.

If it's coming from inside your program, then there's no need to stringify it in the first place.

ScottJ
Additionally, in some circumstances the array will get GCed, at which point the stringified version of the reference can no longer be converted back to a valid array.
outis
Presume I actually wanted to do this. Also the data would still be referenced, I updated the code in the question to reflect this. So garbage collection not an issue.
Tim
@Tim - In an effort to help make your life easier, I would be very interested to know _why_ you want to do this. There may well be a valid reason for doing this, and if so, knock yourself out with the excellent answers provided so far. However, I somehow suspect that there may be a better way to accomplish what you're trying to do, and I guarantee you it will be much more stable and easier to maintain if you find that better way of doing it.
Chris Lutz
@Tim: presuming you actually wanted to do this, "If it's coming from inside your program, then there's no need to stringify it in the first place."
ysth
I wanted to use an array ref as a hash key. I eventually just created a hash with the ref_string->reference. Also it was more of a curiosity for me then an "I can't get this program to work without this functionality" thing. That's why I said "Presume that I want to do it".@Chris thanks for trying to make my life easier - I tracked down a post of yours (on a subjective subject) and gave you an upvote.
Tim
Use Tie::RefHash if you want to use a ref as a hash key. Stringifying is not safe. Neither is "refaddr $ref". If you don't know why, just stick to Tie::RefHash. The corner-cases are properly handled there.
jrockway
+3  A: 

The stringified version contains the memory address of the array object, so yes, you can recover it. This code works for me, anyway (Cygwin, perl 5.8):

use Inline C;
@a = (1,2,3,8,12,17);
$a = \@a . "";
print "Stringified array ref is $a\n";
($addr) = $a =~ /0x(\w+)/;
$addr = hex($addr);
$c = recover_arrayref($addr);
@c = @$c;
print join ":", @c;
__END__
__C__
AV* recover_arrayref(int av_address) { return (AV*) av_address; }

.

$ perl ref-to-av.pl
Stringified array ref is ARRAY(0x67ead8)
1:2:3:8:12:17
mobrule
+9  A: 

Yes, you can do this (even without Inline C). An example:

use strict;
use warnings;

# make a stringified reference
my $array_ref = [ qw/foo bar baz/ ];
my $stringified_ref = "$array_ref";

use B;
# extract the hex address
my ($addr) = $stringified_ref =~ /.*(0x\w+)/;
# fake up a B object of the correct class for this type of reference
# and convert it back to a real reference
my $real_ref = bless(\(0+hex $addr), "B::AV")->object_2svref;

print join(",", @$real_ref), "\n";

but don't do that. If your actual object is freed or reused, you may very well end up getting segfaults.

Whatever you are actually trying to achieve, there is certainly a better way. A comment to another answer reveals that the stringification is due to using a reference as a hash key. As responded to there, the better way to do that is the well-battle-tested Tie::RefHash.

ysth
There are a couple of modules to do this for you, one XS and one similar to the above. But don't use those, either. :)
ysth
Neato .........
mobrule
Well, if you're willing to increment the reference count, it *might* be okay--assuming that it still exists when you get the AV.
Axeman
+2  A: 

I'm not sure why you want to do this, but if you really need it, ignore the answers that use the tricks to look into memory. They'll only cause you problems.

Why do you want to do this? There's probably a better design. Where are you getting that stringified reference from.

Let's say you need to do it for whatever reason. First, create a registry of objects where the hash key is the stringified form, and the value is a weakened reference:

 use Scalar::Util qw(weaken);

 my $array = [ ... ];

 $registry{ $array } = $array;

 weaken( $registry{ $array } ); # doesn't count toward ref count

Now, when you have the stringified form, you just look it up in the hash, checking to see that it's still a reference:

 if( ref $registry{$string} ) { ... }

You could also try Tie::RefHash and let it handle all of the details of this.

There is a longer example of this in Intermediate Perl.

brian d foy
This is what I actually did. Although someone suggested using Tie::RefHash which would have accomplished what I wanted as well. I commented elsewhere that the question was more of a curiosity for me than something I actually needed to do. Thanks for the concern.
Tim