$d = "Nov 25, 1971";
print "\$d contains $d\n"; # "Nov 25, 1971"
print "\$d as unixtime:", $d->to_unixtime, "\n";
####
Can't locate object method "to_unixtime" via package "Nov 25, 1971" (perhaps you forgot to load "Nov 25, 1971"?)
##
##
Datum->tieVar( (my $d), -type=>'date' );
$d = "Nov 25, 1971";
print STDOUT "\$d contains $d\n"; # "Nov 25, 1971"
print STDOUT "\$d as unixtime:", $d->to_unixtime, "\n";
package Datum;
use overload '""' => \&to_string;
sub tieVar {
my $class= shift @_;
my $toTie= \$_[0]; shift @_;
tie $$toTie, $class, @_;
}
sub TIESCALAR {
my $class = shift;
my $instance = shift || undef;
return bless \$instance => $class;
}
sub STORE {
${$_[0]} = $_[1];
}
sub FETCH {
# this just returns "self"
# (won't be good in some cases)
return $_[0];
}
sub to_string {
# this is the "real" FETCH method
return ${$_[0]};
}
sub to_unixtime {
return "foobar";
}
##
##
$d contains Nov 25, 1971
$d as unixtime:foobar