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
Hash::Util::FieldHash::id function, register your object (again, with Hash::Util::FieldHash), and use id_2obj to get the object back. Something like:
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";
You could also
overload the '0+' operator to return the id of your object.
UPDATE: Of course, if you control the object, as per
Corion's
solution, you could just overload numification and stringification on your object instead.
In the case of objects created by other classes, something like this would work:
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"
If you are on a Perl less than 5.10, you should use
Hash::Util::FieldHash::Compat instead, which is a drop-in replacement for
Hash::Util::FieldHash which uses a different method on Perl's before 5.10 and just redirects to
Hash::Util::FieldHash on 5.10+.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.