> I don't see in here how an object tells the difference between being in
> scalar context and being in string context.
Think tie(). It wouldn't necessarily be the object that makes the
decision.
> for anything that wants to take such a relatively odd action.
As Spider notes, this isn't really an "odd action". It's the only way to
easily support this, returning both an object and a scalar:
$scalar = date; # scalar ctime date, same as localtime()
$object = date; # date object with accessor functions
without some special syntax or explicit casting.
Consider lots of other functions and how they could benefit if the
"scalar" was really just a front-end to an object:
$pw = getpwnam('nwiger');
print "$pw"; # calls $pw->SCALAR, which prints 'nwiger'
die "Bad group" unless $pw->gid == 100;
Note we don't have to go thru a big list, picking off the values we want
anymore. Yet, we still have direct access to what the routine's
"supposed" to return in scalar context.
There is an amazing amount of power in this approach and, as Spider
notes, this is something that's actually been on Larry's wishlist for
sometime. I think Perl 6 could benefit enormously from it.
-Nate