NAME

Wanted - Extended caller context detection

SYNOPSIS

use Wanted;
sub foo :lvalue
{
    if ( want( qw'LVALUE ASSIGN' ) )
    {
        print "We have been assigned ", want('ASSIGN');
        lnoreturn;
    }
    elsif( want('LIST') )
    {
        rreturn (1, 2, 3);
    }
    elsif( want('BOOL') )
    {
        rreturn 0;
    }
    elsif( want(qw'SCALAR !REF') )
    {
        rreturn 23;
    }
    elsif( want('HASH') )
    {
        rreturn { foo => 17, bar => 23 };
    }
    # You have to put this at the end to keep the compiler happy
    return;
}

foo() = 23;  # Assign context
@x = foo();  # List context
if( foo() )  # Boolean context
{
    print( "Not reached\n" );
}

Also works in threads, where the context is set at thread creation.

require threads;
# In scalar context
my $thr = threads->create(sub
{
    return( want('SCALAR') );
});
my $is_scalar = $thr->join; # true

# or
my $thr = threads->create({ context => 'scalar' }, sub
{
    return( want('SCALAR') );
});
my $is_scalar = $thr->join; # true

my( $thr ) = threads->create(sub
{
    return( want('LIST') );
});
my @list_result = $thr->join;
# $list_result[0] is true

# or
my $thr = threads->create({ context => 'list' }, sub
{
    return( want('LIST') );
});
my @list_result = $thr->join;
# $list_result[0] is true

# Force the context by being explicit:
my $thr = threads->create({ context => 'void' }, sub
{
    return( want('VOID') ? 1 : 0 );
});
my $is_void = $thr_void->join; # undef

VERSION

v0.1.0

DESCRIPTION

This XS module generalises the mechanism of the wantarray function, allowing a function to determine in detail how its return value is going to be used.

It is a fork from the module Want, by Robin Houston, that is not updated anymore since 2016, and that throws a segmentation fault when called from the last line of a thread, or from a tie method, or from the last line of a mod_perl handler, when there is a lack of context.

To install this module, run the following commands:

perl Makefile.PL
make
make test
make install

Top-level contexts:

The three kinds of top-level context are well known:

Lvalue subroutines:

The introduction of lvalue subroutines in Perl 5.6 has created a new type of contextual information, which is independent of those listed above. When an lvalue subroutine is called, it can either be called in the ordinary way (so that its result is treated as an ordinary value, an rvalue); or else it can be called so that its result is considered updatable, an lvalue.

These rather arcane terms (lvalue and rvalue) are easier to remember if you know why they are so called. If you consider a simple assignment statement left = right, then the left-hand side is an lvalue and the right-hand side is an rvalue.

So (for lvalue subroutines only) there are two new types of context:

Assignment context:

The commonest use of lvalue subroutines is with the assignment statement:

size() = 12;
(list()) = (1..10);

A useful motto to remember when thinking about assignment statements is context comes from the left. Consider code like this:

my ($x, $y, $z);
sub list () :lvalue { ($x, $y, $z) }
list = (1, 2, 3);
print "\$x = $x; \$y = $y; \$z = $z\n";

This prints $x = ; $y = ; $z = 3, which may not be what you were expecting. The reason is that the assignment is in scalar context, so the comma operator is in scalar context too, and discards all values but the last. You can fix it by writing (list) = (1,2,3); instead.

If your lvalue subroutine is used on the left of an assignment statement, it is in ASSIGN context. If ASSIGN is the only argument to want(), then it returns a reference to an array of the value(s) of the right-hand side.

In this case, you should return with the lnoreturn function, rather than an ordinary return.

This makes it very easy to write lvalue subroutines which do clever things:

   use Wanted;
   use strict;
   sub backstr :lvalue {
       if (want(qw'LVALUE ASSIGN')) {
           my ($a) = want('ASSIGN');
           $_[0] = reverse $a;
           lnoreturn;
       }
       elsif (want('RVALUE')) {
           rreturn scalar reverse $_[0];
       }
       else {
           carp("Not in ASSIGN context");
       }
       return
   }

   print "foo -> ", backstr("foo"), "\n";        # foo -> oof
   backstr(my $robin) = "nibor";
   print "\$robin is now $robin\n";              # $robin is now robin

Notice that you need to put a (meaningless) return statement at the end of the function, otherwise you will get the error Can't modify non-lvalue subroutine call in lvalue subroutine return.

The only way to write that backstr function without using Want is to return a tied variable which is tied to a custom class.

Reference context:

Sometimes in scalar context the caller is expecting a reference of some sort to be returned:

print foo()->();     # CODE reference expected
print foo()->{bar};  # HASH reference expected
print foo()->[23];   # ARRAY reference expected
print ${foo()};      # SCALAR reference expected
print foo()->bar();  # OBJECT reference expected

my $format = *{foo()}{FORMAT} # GLOB reference expected

You can check this using conditionals like if (want('CODE')). There is also a function wantref() which returns one of the strings CODE, HASH, ARRAY, GLOB, SCALAR or OBJECT; or the empty string if a reference is not expected.

Because want('SCALAR') is already used to select ordinary scalar context, you have to use want('REFSCALAR') to find out if a SCALAR reference is expected. Or you could use want('REF') eq 'SCALAR' of course.

Be warned that want('ARRAY') is a very different thing from wantarray().

Item count

Sometimes in list context the caller is expecting a particular number of items to be returned:

my ($x, $y) = foo(); # foo is expected to return two items

If you pass a number to the want function, then it will return true or false according to whether at least that many items are wanted. So if we are in the definition of a sub which is being called as above, then:

want(1) returns true
want(2) returns true
want(3) returns false

Sometimes there is no limit to the number of items that might be used:

my @x = foo();
do_something_with( foo() );

In this case, want(2), want(100), want(1E9) and so on will all return true; and so will want('Infinity').

The howmany function can be used to find out how many items are wanted. If the context is scalar, then want(1) returns true and howmany() returns 1. If you want to check whether your result is being assigned to a singleton list, you can say if (want('LIST', 1)) { ... }.

Boolean context

Sometimes the caller is only interested in the truth or falsity of a function's return value:

if (everything_is_okay()) {
    # Carry on
}

print (foo() ? "ok\n" : "not ok\n");

In the following example, all subroutine calls are in BOOL context:

my $x = ( (foo() && !bar()) xor (baz() || quux()) );

Boolean context, like the reference contexts above, is considered to be a subcontext of SCALAR.

FUNCTIONS

want(SPECIFIERS)

This is the primary interface to this module, and should suffice for most purposes. You pass it a list of context specifiers, and the return value is true whenever all of the specifiers hold.

want('LVALUE', 'SCALAR');   # Are we in scalar lvalue context?
want('RVALUE', 3);          # Are at least three rvalues wanted?
want('ARRAY');              # Is the return value used as an array ref?

You can also prefix a specifier with an exclamation mark to indicate that you do not want it to be true

want(2, '!3');              # Caller wants exactly two items.
want(qw'REF !CODE !GLOB');  # Expecting a reference that is not a CODE or GLOB ref.
want(100, '!Infinity');     # Expecting at least 100 items, but there is a limit.

If the REF keyword is the only parameter passed, then the type of reference will be returned. This is just a synonym for the wantref function: it is included because you might find it useful if you do not want to pollute your namespace by importing several functions, and to conform to Damian Conway's suggestion in RFC 21.

Finally, the keyword COUNT can be used, provided that it is the only keyword you pass. Mixing COUNT with other keywords is an error. This is a synonym for the "howmany" function.

A full list of the permitted keyword is in the "ARGUMENTS" section below.

rreturn

Use this function instead of return from inside an lvalue subroutine when you know that you are in RVALUE context. If you try to use a normal return, you will get a compile-time error in Perl 5.6.1 and above unless you return an lvalue. (Note: this is no longer true in Perl 5.16, where an ordinary return will once again work.)

lnoreturn

Use this function instead of return from inside an lvalue subroutine when you are in ASSIGN context and you have used want('ASSIGN') to carry out the appropriate action.

If you use "rreturn" or "lnoreturn", then you have to put a bare return; at the very end of your lvalue subroutine, in order to stop the Perl compiler from complaining. Think of it as akin to the 1; that you have to put at the end of a module. (Note: this is no longer true in Perl 5.16.)

howmany()

Returns the expectation count, i.e. the number of items expected. If the expectation count is undefined, that indicates that an unlimited number of items might be used (e.g. the return value is being assigned to an array). In void context the expectation count is zero, and in scalar context it is one.

The same as want('COUNT').

wantref()

Returns the type of reference which the caller is expecting, or the empty string if the caller is not expecting a reference immediately.

The same as want('REF').

context

INTERNAL FUNCTIONS

The following functions are internal to Wanted and are not intended for public use. They are documented here for reference but should not be relied upon in user code, as their behaviour or availability may change in future releases.

wantassign

want_assign

want_boolean

want_count

want_lvalue

EXAMPLES

use Wanted 'howmany';
sub numbers
{
    my $count = howmany();
    die( "Cannot make an infinite list" ) if( !defined( $count ) );
    return( 1..$count );
}
my( $one, $two, $three ) = numbers();

use Wanted 'want';
sub pi ()
{
    if( want('ARRAY') )
    {
        return( [3, 1, 4, 1, 5, 9] );
    }
    elsif( want('LIST') )
    {
        return( 3, 1, 4, 1, 5, 9 );
    }
    else
    {
        return(3);
    }
}
print pi->[2];      # prints 4
print ((pi)[3]);    # prints 1

ARGUMENTS

The permitted arguments to the want function are listed below. The list is structured so that sub-contexts appear below the context that they are part of.

EXPORT

The "want" and "rreturn" functions are exported by default.

The "wantref" and/or "howmany" functions can also be imported:

use Wanted qw( want howmany );

If you do not import these functions, you must qualify their names as (e.g.) Wanted::wantref.

SUBTLETIES

There are two different levels of BOOL context. Pure boolean context occurs in conditional expressions, and the operands of the xor and !/not operators.

Pure boolean context also propagates down through the && and || operators.

However, consider an expression like my $x = foo() && "yes". The subroutine is called in pseudo-boolean context - its return value is not entirely ignored, because the undefined value, the empty string and the integer 0 are all false.

At the moment want('BOOL') is true in either pure or pseudo boolean context.

CREDITS

Robin Houston, robin@cpan.org wrote the original module Want on which this is based.

Also, credits to Grok from xAI for its support in updating the XS code, providing unit tests, and helping resolve several bugs from the original Want module.

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

"wantarray" in perlfunc, Perl6 RFC 21, by Damian Conway

Contextual::Call, Contextual::Diag, Contextual::Return

COPYRIGHT & LICENSE

Copyright(c) 2025 DEGUEST Pte. Ltd.

Portions copyright (c) 2001-2016, Robin Houston.

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.