Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Contextual::Return blocks: can they be made to return the actual result?

by tobyink (Abbot)
on Jul 13, 2013 at 15:53 UTC ( #1044163=note: print w/ replies, xml ) Need Help??


in reply to Contextual::Return blocks: can they be made to return the actual result?

No; Contextual::Return always returns a Contextual::Return::Value object in scalar context. That's part of the mechanism that allows it to work; when the function is running it can't predict, say, whether the result will be used as a string or a boolean, so it returns an object that overloads both.

If $foo is an object, you shouldn't be peeking at $foo->{name} anyway; write an accessor so that you can do $foo->name.

If you're after wantarray-on-crack, then Want might be more to your liking.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: Contextual::Return blocks: can they be made to return the actual result?
Select or Download Code
Re^2: Contextual::Return blocks: can they be made to return the actual result?
by wanna_code_perl (Pilgrim) on Jul 14, 2013 at 03:00 UTC
    If $foo is an object, you shouldn't be peeking at $foo->{name} anyway; write an accessor so that you can do $foo->name.

    Funny you should mention that. That's precisely what I was trying to do when I first saw the "DEFAULT block did not return a suitable reference to the hash dereference at ...". I decided it wasn't worth it a short time later, but it was starting to look like I'd have to implement most of the class inside one of Contextual::Return's METHOD { ... } blocks.

    You're spot on, though. If I use anything, it'll probably be Want.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-07-30 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls