in reply to Recovering References from Interpolation
The only problem I have with that is why you would do something like this anyway:
Is there any use for this? If you really want a stringified reference, you can always make a copy (ie:)my $ref = Object->new(); $ref = "$ref"; # overwrite $ref: destroy object
Update: Just a reminder: you can not restore a pointer to a destroyed object without serious memory leeks / core dumps and other unpleasantness, so IF the object is still in memory, you still have another reference laying about somewhere - then you CAN get your reference back.my $ref = Object->new(); my $refstring = "$ref"; # keep object in $ref
#!/usr/bin/perl -w use strict; use vars qw($ref); $ref = [1,2,3,4]; print join(',',@$ref)."\n"; my $string = "$ref"; #print join(',',@$string)."\n"; # this does not work my $getref = unstringify($string) || die "Cannot find ref!"; print join(',',@$getref)."\n"; sub unstringify { # works only on globals in package main ! # but you could also use another hash # ( instead of %main:: ) my $string = shift; for (keys %{main::}) { my $value; next unless $value = ${${main::}{$_}}; next unless ref($value); next unless $value eq $string; # implicit stringification return $value; } return; }
This is ofcourse a terribly hack, you'd be a lot better of if you store your references somewhere where you can retrieve them easier.
-- Joost downtime n. The period during which a system is error-free and immune from user input.
|
|---|