| 1 |
----------------------------------------------------------------------------- |
| 2 |
| Want v0.29 - Robin Houston, 2016-02-26 |
| 3 |
----------------------------------------------------------------------------- |
| 4 |
|
| 5 |
For full documentation, see the POD included with the module. |
| 6 |
Below is a brief extract of the documentation, to give you an |
| 7 |
idea of what this module does. It requires Perl version 5.6 or |
| 8 |
later. |
| 9 |
|
| 10 |
|
| 11 |
NAME |
| 12 |
Want - Implement the `want' command |
| 13 |
|
| 14 |
SYNOPSIS |
| 15 |
use Want; |
| 16 |
sub foo :lvalue { |
| 17 |
if (want(qw'LVALUE ASSIGN')) { |
| 18 |
print "We have been assigned ", want('ASSIGN'); |
| 19 |
lnoreturn; |
| 20 |
} |
| 21 |
elsif (want('LIST')) { |
| 22 |
rreturn (1, 2, 3); |
| 23 |
} |
| 24 |
elsif (want('BOOL')) { |
| 25 |
rreturn 0; |
| 26 |
} |
| 27 |
elsif (want(qw'SCALAR !REF')) { |
| 28 |
rreturn 23; |
| 29 |
} |
| 30 |
elsif (want('HASH')) { |
| 31 |
rreturn { foo => 17, bar => 23 }; |
| 32 |
} |
| 33 |
return |
| 34 |
} |
| 35 |
|
| 36 |
DESCRIPTION |
| 37 |
This module generalises the mechanism of the wantarray |
| 38 |
function, allowing a function to determine in some detail |
| 39 |
how its return value is going to be immediately used. |
| 40 |
|
| 41 |
... |
| 42 |
|
| 43 |
EXAMPLES |
| 44 |
|
| 45 |
use Carp 'croak'; |
| 46 |
use Want 'howmany'; |
| 47 |
sub numbers { |
| 48 |
my $count = howmany(); |
| 49 |
croak("Can't make an infinite list") if !defined($count); |
| 50 |
return (1..$count); |
| 51 |
} |
| 52 |
my ($one, $two, $three) = numbers(); |
| 53 |
|
| 54 |
|
| 55 |
use Want 'want'; |
| 56 |
sub pi () { |
| 57 |
if (want('ARRAY')) { |
| 58 |
return [3, 1, 4, 1, 5, 9]; |
| 59 |
} |
| 60 |
elsif (want('LIST')) { |
| 61 |
return (3, 1, 4, 1, 5, 9); |
| 62 |
} |
| 63 |
else { |
| 64 |
return 3; |
| 65 |
} |
| 66 |
} |
| 67 |
print pi->[2]; # prints 4 |
| 68 |
print ((pi)[3]); # prints 1 |
| 69 |
|
| 70 |
|
| 71 |
use Want; |
| 72 |
use strict; |
| 73 |
sub backstr :lvalue { |
| 74 |
if (want(qw'LVALUE ASSIGN')) { |
| 75 |
my ($a) = want('ASSIGN'); |
| 76 |
$_[0] = reverse $a; |
| 77 |
lnoreturn; |
| 78 |
} |
| 79 |
elsif (want('RVALUE')) { |
| 80 |
rreturn scalar reverse $_[0]; |
| 81 |
} |
| 82 |
else { |
| 83 |
carp("Not in ASSIGN context"); |
| 84 |
} |
| 85 |
return |
| 86 |
} |
| 87 |
|
| 88 |
print "foo -> ", backstr("foo"), "\n"; # foo -> oof |
| 89 |
backstr(my $robin) = "nibor"; |
| 90 |
print "\$robin is now $robin\n"; # $robin is now robin |
| 91 |
|
| 92 |
|
| 93 |
AUTHOR |
| 94 |
Robin Houston, <[email protected]> |
| 95 |
|
| 96 |
Thanks to Damian Conway for encouragement and good |
| 97 |
suggestions, and to Father Chrysostomos and Matthew |
| 98 |
Horsfall for patches. |
| 99 |
|
| 100 |
SEE ALSO |
| 101 |
o the wantarray entry in the perlfunc manpage |
| 102 |
|
| 103 |
o Perl6 RFC 21, by Damian Conway. |
| 104 |
http://dev.perl.org/rfc/21.html |
| 105 |
|
| 106 |
COPYRIGHT |
| 107 |
Copyright (c) 2001-2012, Robin Houston. All Rights Reserved. |
| 108 |
This module is free software. It may be used, redistributed |
| 109 |
and/or modified under the same terms as Perl itself. |