Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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+.

In reply to Re: dualvar for a reference-string combination by james2vegas
in thread dualvar for a reference-string combination by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found