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:

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 }
(note the "rreturn" statements, explained in the doc).

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??


In reply to Contextual::Return and Want, any major difference? by mascip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.