this isn't completely possible. no matter what you do with tie, bless, etc. the following lines won't work as you expect:
$d = "Nov 25, 1971"; print "\$d contains $d\n"; # "Nov 25, 1971" print "\$d as unixtime:", $d->to_unixtime, "\n";

with a tie, you can't directly do $d->to_unixtime, it has to be tied($d)->to_unixtime. otherwise you'll get the message: Can't locate object method "to_unixtime" via package "Nov 25, 1971" (perhaps you forgot to load "Nov 25, 1971"?)

with a regular object, you can't of course do $d = "Nov 25, 1971" because this would destroy the object previously assigned to $d.

the most functional hack I could come up with is the use of tie AND overload AND tye's suggestion, as in:

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"; }
this prints what you expect:
$d contains Nov 25, 1971 $d as unixtime:foobar
...but this is a hack, as it works correctly only when fetching $d in a string context. it has the implicit advantage, however, that $d could return something different (perhaps $d->to_unixtime) in numeric context.

cheers,
Aldo

King of Laziness, Wizard of Impatience, Lord of Hubris


In reply to Re: How might I return tied scalar that does not have to be dereferenced as $$scalar? by dada
in thread How might I return tied scalar that does not have to be dereferenced as $$scalar? by rr

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.