Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Contextual::Return and Want, any major difference?

by duelafn (Priest)
on Mar 03, 2012 at 14:47 UTC ( #957683=note: print w/ replies, xml ) Need Help??


in reply to Contextual::Return and Want, any major difference?

Want dispatches based on immediate context, whereas Contextual::Return dispatches based on first use. Example:

#!/usr/bin/perl use warnings; use strict; use Contextual::Return; use Want; sub foo_want { if (want('HASH')) { return { foo => "bar" } } else { return "Hello" } } sub foo_ctx { return HASHREF { { foo => "bar" } } DEFAULT { "Hello" } } print foo_ctx()->{foo}, "\n"; print foo_want()->{foo}, "\n"; my $ctx = foo_ctx(); my $want = foo_want(); print $ctx->{foo}, "\n"; print $want->{foo}, "\n";

And the result

$ perl /tmp/test.pl bar bar bar Can't use string ("Hello") as a HASH ref while "strict refs" in use at + /tmp/test.pl line 22.

In the second case, Want "fails" because it was not called in hashref context.

Note: rreturn only needed in :lvalue subs

Update: Showed that the want code works when used inline.

Good Day,
    Dean


Comment on Re: Contextual::Return and Want, any major difference?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-10-01 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (7 votes), past polls