You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/5to6-perlfunc.pod
+68-1Lines changed: 68 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1727,7 +1727,74 @@ As with C<wait>, the disposition of this is unclear.
1727
1727
1728
1728
=itemwantarray
1729
1729
1730
-
There is no C<wantarray> in Perl 6.
1730
+
There is no C<wantarray> in Perl 6, because L<reasons|/language/faq#Why_is_wantarray_or_want_gone?_Can_I_return_different_things_in_different_contexts?>.
1731
+
1732
+
There are very easy ways to cover many of the use cases which wantarray filled.
1733
+
1734
+
First, since Perl 6 does not need special reference syntax to contain
1735
+
a C<List> or C<Array> in a C<Scalar>, simply returning a list may be
1736
+
all that is needed:
1737
+
1738
+
sub listofstuff {
1739
+
return 1,2,3;
1740
+
}
1741
+
my $a = listofstuff();
1742
+
print $a; # prints "123"
1743
+
print join("<",listofstuff()) # prints "1<2<3"
1744
+
1745
+
One of the most common use cases is to provide either an array of lines
1746
+
or elements, or a prettier string than would be produced by simply
1747
+
printing the array. One can mix in a custom C<.Str> method for this
1748
+
purpose:
1749
+
1750
+
sub prettylist(*@origlist) {
1751
+
@origlist but role {
1752
+
method Str { self.join("<") }
1753
+
}
1754
+
}
1755
+
print prettylist(1,2,3); # prints "1<2<3"
1756
+
print join(">", prettylist(3,2,1)); # prints "3>2>1"
1757
+
1758
+
In the above example, the returned list may be lazy, and the C<.Str> method
1759
+
is not called until stringification happens, so no extra work is done
1760
+
to generate something which is not asked for.
1761
+
1762
+
Another use case is to create methods which are mutators when called
1763
+
in void context but produce copies during assignment. It is generally
1764
+
considered better form in Perl 6 not to do so, since users can quite
1765
+
easily turn any copy-producing method into a mutator using the C<.=>
1766
+
operator:
1767
+
1768
+
my $a = "foo\n";
1769
+
$a.ords.say; # says "(102 111 111 10)"
1770
+
$a .= chomp;
1771
+
$a.ords.say; # says "(102 111 111)"
1772
+
1773
+
However if you have your heart set on using the same function
1774
+
name for both operations, you can get most of the way there by mixing in
1775
+
a C<.sink> method, which will be called when the result finds itself
1776
+
in void context. There are some caveats however, so again, this is
1777
+
not advised:
1778
+
1779
+
multi sub increment($b is rw) {
1780
+
($b + 1) does role { method sink { $b++ } }
1781
+
}
1782
+
multi sub increment($b) {
1783
+
$b + 1
1784
+
}
1785
+
my $a = 1;
1786
+
increment($a);
1787
+
say $a; # says "2"
1788
+
my $b = increment($a);
1789
+
say $a, $b; # says "2 3"
1790
+
# ...users will just have to be aware that they should not accidentally
1791
+
# sink a stored value later, though this requires some effort to
0 commit comments