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:

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

Replies are listed 'Best First'.
Re: Contextual::Return and Want, any major difference?
by duelafn (Parson) on Mar 03, 2012 at 14:47 UTC

    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

Re: Contextual::Return and Want, any major difference?
by moritz (Cardinal) on Mar 03, 2012 at 14:47 UTC

      Wow, that IS a big difference, thank you Dean.
      That makes me want to use Contextual::Return.

      Apparently Contextual::Return also enables to use "active" values, by using the VALUE keyword.

Re: Contextual::Return and Want, any major difference?
by rpg (Novice) on Jun 28, 2012 at 10:27 UTC
    Contextual::Return itself uses Want module :)