in reply to Re^2: OO design: returning string and/or special value
in thread OO design: returning string and/or special value
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: OO design: returning string and/or special value
by wanna_code_perl (Friar) on Oct 07, 2019 at 22:31 UTC | |
by AnomalousMonk (Archbishop) on Oct 07, 2019 at 23:52 UTC |