NAME
Want - Implement the want
command
SYNOPSIS
use Want ('want');
sub foo :lvalue {
if (want('lvalue')) {
return $x;
}
elsif (want->{LIST}) {
return (1, 2, 3);
}
DESCRIPTION
This module generalises the mechanism of the wantarray function, allowing a function to determine in some detail how its return value is going to be immediately used.
Top-level contexts:
The three kinds of top-level context are well known:
- VOID
-
The return value is not being used in any way; the function call is an entire statement:
foo();
- SCALAR
-
The return value is being treated as a scalar value of some sort:
my $x = foo(); $y += foo(); print "123" x foo(); print scalar foo(); warn foo()->{23}; ...etc...
- LIST
-
The return value is treated as a list of values:
my @x = foo(); my ($x) = foo(); () = foo(); # even though the results are discarded print foo(); bar(foo()); # unless the bar subroutine has a prototype print @hash{foo()}; # (hash slice) ...etc...
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:
- RVALUE
-
The caller is definitely not trying to assign to the result:
foo(); my $x = foo(); ...etc...
- LVALUE
-
Either the caller is directly assigning to the result of the sub call:
foo() = $x; foo() = (1, 1, 2, 3, 5, 8);
or the caller is making a reference to the result, which might be assigned to later:
my $ref = \(foo()); # Could now have: $$ref = 99; # Note that this example imposes LIST context on the sub call. # So we're taking a reference to the first element to be # returned _in list context_. # If we want to call the function in scalar context, we can # do it like this: my $ref = \(scalar foo());
or else the result of the function call is being used as part of the argument list for another function call:
bar(foo()); # Will *always* call foo in lvalue context, # regardless of what bar actually does.
The reason for this last case is that bar might be a sub which modifies its arguments. They're rare in contemporary Perl code, but still possible:
sub bar { $_[0] = 23; }
Reference context:
Sometimes in list 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
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" or "GLOB"; or the empty string if a reference is not expected.
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
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)) { ... }
.
EXAMPLES
use Carp 'croak';
use Want 'howmany';
sub numbers {
my $count = howmany();
croak("Can't make an infinite list") if !defined($count);
return (1..$count);
}
my ($one, $two, $three) = numbers();
use Want '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
EXPORT
None by default. The want
, wantref
and/or howmany
functions can be imported:
use Want qw'want howmany';
If you don't import these functions, you must qualify their names as (e.g.) Want::want
.
INTERFACE
This is the first release of this module, and the public interface may change in future versions. It's too early to make any guarantees about interface stability.
I'd be interested to know how you're using this module.
AUTHOR
Robin Houston, <robin@kitsite.com>
SEE ALSO
Perl6 RFC 21, by Damian Conway. http://dev.perl.org/rfc/21.html
COPYRIGHT
Copyright (c) 2001, Robin Houston. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.