note
james2vegas
Your object is going to get numified, so it will try and numify the reference (usually to 0, since it ill start with ARRAY or HASH). Perhaps it would be better to set numeric part of dualvar the [mod://Hash::Util::FieldHash]::id function, register your object (again, with Hash::Util::FieldHash), and use id_2obj to get the object back. Something like:<br><br>
<code>
use strict;
use warnings;
use Scalar::Util qw/dualvar/;
use Hash::Util::FieldHash qw/id id_2obj register/;
{
package Baz;
use Hash::Util::FieldHash qw/register/;
sub new {
my $class = shift;
my $self = bless {}, $class;
register $self;
$self
}
sub test {
'In object Baz';
}
}
my $b = Baz->new;
my $x = dualvar( id( $b ), 'Baz says Hi' );
print "$x - ", (id_2obj($x+0))->test(),"\n";
</code>
<br>You could also [mod://overload|overload] the '0+' operator to return the id of your object.<br><br>
UPDATE: Of course, if you control the object, as per [Corion]'s [id://914987|solution], you could just overload numification and stringification on your object instead.
<br><br>
In the case of objects created by other classes, something like this would work:<br>
<code>
use strict;
use warnings;
use Scalar::Util qw/dualvar/;
use Hash::Util::FieldHash qw/id id_2obj register/;
use Time::Piece;
register my $t = Time::Piece::localtime; # Time::Piece object
my $x = dualvar( id( $t ), "$t" );
print "$x = ", (id_2obj($x+0))->epoch, " seconds since the epoch\n"
</code>
If you are on a Perl less than 5.10, you should use [mod://Hash::Util::FieldHash::Compat] instead, which is a drop-in replacement for [mod://Hash::Util::FieldHash] which uses a different method on Perl's before 5.10 and just redirects to [mod://Hash::Util::FieldHash] on 5.10+.
914984
914984