OK I think I misread / elided some of your OP where you said that both values were strings. In that case C::R probably isn't what you're wanting, unless you wanted to return a magical hashref that had both values that stringified to the string bit (disclaimer I've never really used C::R in anger, just aware of it's presence).
use Contextual::Return;
sub blahblah {
## ...
return (
STR { _special_to_string( $retval ); }
HASHREF { +{ special => $retval, string => _special_to_string( $re
+tval ) }; }
);
}
my $foo = blahblah( @some_args );
say "Foo: $foo";
say "Special from foo: ", $foo->{special};
There's also Object::Result which is a variation on the theme which is aimed at making it "easier" to return an instance with arbitrary methods; I believe it's mentioned/recommended in the updated PBP course, for whatever that's worth.
After thinking over it a bit more I think I'd lean towards just a hashref, maybe Object::Result; and eschew overloading or other magic. If you name the slot with the plain string well you probably win in clarity what you lose in extra typing (say $result->{message}).
Then again I drove 900+ miles this weekend so take that with a large grain of salt.</handwaving>
The cake is a lie.
The cake is a lie.
The cake is a lie.
|