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


in reply to dualvar for a reference-string combination

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