Skip to content

Commit 4818aa3

Browse files
committed
Explain P5 -> P6 difference for shift/unshift better
1 parent 5a656e4 commit 4818aa3

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

doc/Language/5to6-perlfunc.pod6

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ and from processes.
11161116
Works in Perl 6, and can also be used as a method. I. e. C<my $x = pop
11171117
@a;> and C<my $x = @a.pop;> are equivalent.
11181118
1119-
The non-paramater version of C<pop> does not exist. Also, if the array
1119+
The non-parameter version of C<pop> does not exist. Also, if the array
11201120
is empty, a Failure will be returned in Perl 6, which will throw if the
11211121
value is actually used in a significant way.
11221122
@@ -1492,8 +1492,25 @@ Not documented, but probably hiding in an C<IO> class somewhere.
14921492
14931493
=item shift
14941494
1495-
Works as a method as well as a function. C<shift @a> and C<@a.shift> are
1496-
equivalent.
1495+
Works in Perl 6, and can also be used as a method. I. e. C<my $x = shift
1496+
@a;> and C<my $x = @a.shift;> are equivalent.
1497+
1498+
The non-parameter version of C<shift> does not exist. Also, if the array
1499+
is empty, a Failure will be returned in Perl 6, which will throw if the
1500+
value is actually used in a significant way.
1501+
1502+
If you are using only defined values in your array, you can use the C<with>
1503+
function to handle this case:
1504+
1505+
with shift @array -> $shifted {
1506+
say "shifted '$shifted' of the array";
1507+
}
1508+
else {
1509+
say "there was nothing to shift";
1510+
}
1511+
1512+
The Perl 6 ecosystem has a module C<P5shift> which exports a C<shift>
1513+
function that mimics the original Perl 5 behaviour as much as possible.
14971514
14981515
=head2 shm*
14991516
@@ -1919,8 +1936,16 @@ Perl 6 version.
19191936
19201937
=item unshift EXPR, LIST
19211938
1922-
Available in Perl 6. Can be used as a method. C<unshift(@a, "blah")> is
1923-
equivalent to C<@a.unshift("blah")>.
1939+
Works as in Perl 5, as well as being available as a method:
1940+
C<@a.unshift("foo");>. I<Note:> the flattening behaviour is different in Perl 6:
1941+
C<@b.unshift: @a> will unshift C<@a> into C<@b> as a single element. See also the
1942+
L<prepend method|/type/Array#method_prepend>.
1943+
1944+
Also note that C<unshift> in Perl 6 returns the array to which was pushed,
1945+
contrary to Perl 5 where it returns the new number of elements.
1946+
1947+
The Perl 6 ecosystem has a module C<P5shift> which exports a C<unshift>
1948+
function that mimics the original Perl 5 behaviour as much as possible.
19241949
19251950
=head2 untie
19261951

0 commit comments

Comments
 (0)