Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: dualvar for a reference-string combination

by james2vegas (Chaplain)
on Jul 17, 2011 at 19:01 UTC ( #914991=note: print w/replies, xml ) Need Help??

in reply to dualvar for a reference-string combination

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+.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://914991]
[erix]: can you drop a name? :)
[karlgoethebier]: Corion: "...didn't leave business cards" Those guys came from Muränia?
[Corion]: karlgoethebier: Heh - naah, I think they are just very fresh out of university. At least they seem to have hired one person as key accountant who has done enterprise consulting etc. before :)
[Corion]: erix: It wouldn't be nice of me to publically shame and name ;)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2018-04-19 12:23 GMT
Find Nodes?
    Voting Booth?