mascip has asked for the wisdom of the Perl Monks concerning the following question:
Hi :o),
Contextual::Return and Want seem very similar : they both enable to behave differently depending on the context in which a subroutine is called. Like 'wantarray', but they do more as they know the difference between string, boolean, list, hash, arrayref, etc.
* Here is a simple example using Contextual::Return :
sub bla { return ( LIST { (1, 2, 3) } BOOL { 1 } HASH { { foo => 17, bar => 23 } } ); }
* Here a simple example using Want:
(note the "rreturn" statements, explained in the doc).sub bli { if (want('LIST')) { return (1, 2, 3); } elsif (want('BOOL')) { rreturn 0; } elsif (want('HASH')) { rreturn { foo => 17, bar => 23 }; } return; # You have to put this at the end # to keep the compiler happy }
I hope i didn't do any mistake. And i guess it might be possible to make the code shorter for Want, by putting the "if" controls at the end of the "return" lines.
the differences that i have seen are that :
- Contextual::Return only enables to RETURN something different, while Want enables to behave differently anywhere in the code (with 'if' statements).
- Want's syntax is less readable. I guess it is also because it can do more?
- i read that Contextual::Return is slow. What about Want ?
Please don't discuss whether Contextual::Return is too slow or not : i read it in several places already. And while it is not ok for some program to be slow, it can be ok for others. For example if the bottleneck of the application doesn't reside in the code, but in the external environment, for example internet.
So, any other differences ? Has anybody tried both??
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Contextual::Return and Want, any major difference?
by duelafn (Parson) on Mar 03, 2012 at 14:47 UTC | |
|
Re: Contextual::Return and Want, any major difference?
by moritz (Cardinal) on Mar 03, 2012 at 14:47 UTC | |
by mascip (Pilgrim) on Mar 03, 2012 at 15:02 UTC | |
|
Re: Contextual::Return and Want, any major difference?
by rpg (Novice) on Jun 28, 2012 at 10:27 UTC |