http://www.perlmonks.org?node_id=914984

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a variable, I need to access sometimes as a string and sometimes doing more sophistaced things as a module, The following, well simplified example, shoud then print out two times a date - which not have to be the same.

my $a = dualvar(DateTime->now, "2011/07/17"); print "$a\n".$a->mdy("/")."\n";

Using Scalar::Util's dualvar it is possible to store a string and a number in one var. According to the documentation, this should be possible for storing a string and a reference, too.

So, before getting the hands dirty on writing another module, is there a current implementation for this?

Replies are listed 'Best First'.
Re: dualvar for a reference-string combination
by Corion (Patriarch) on Jul 17, 2011 at 18:37 UTC

    Can you maybe do with just using overload? Then you can control what happens when your object is used as a string, used as a number and other things... Your example would fit this usage:

    package My::DateObject; use DateTime; use vars '$AUTOLOAD'; use Carp qw(croak); use overload q{""} => 'as_string'; sub new { my ($class, %args) = @_; my $self = { dt => DateTime->now(), %args, }; bless $self => $class; }; sub as_string { $_[0]->{ dt }->ymd("-"); }; sub DESTROY {}; # we have AUTOLOAD sub AUTOLOAD { # blindly delegate to $self->{dt} $AUTOLOAD =~ /::(\w+)$/ or croak "Malformed method '$AUTOLOAD'"; my $method = $1; $_[0]->{dt}->$method(@_); }; package main; my $do = My::DateObject->new(); print "As string: $do\n"; print "As object: ", $do->mdy("/"),"\n";

    Update: Fixed lots of typos in the untested code

Re: dualvar for a reference-string combination
by dave_the_m (Monsignor) on Jul 17, 2011 at 20:59 UTC
    Note that a dualvar-style solution isn't possible, since the RV and PV fields in an SV share the same physical slot.

    Dave.

Re: dualvar for a reference-string combination
by james2vegas (Chaplain) on Jul 17, 2011 at 19:01 UTC
    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+.
Re: dualvar for a reference-string combination
by Anonymous Monk on Jul 17, 2011 at 18:40 UTC
    Path::Class is on module which does this. Check out its use of overload at the beginning of Entity.pm, where it designated its stringify method as the handler for stringification.
Re: dualvar for a reference-string combination
by Anonymous Monk on Jul 17, 2011 at 19:09 UTC

    I thank you for your replies. By glancing over overload I stop at the operators and missed the conversion-features.