This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlfunc: replace splice example with simpler one
authorRicardo Signes <rjbs@cpan.org>
Fri, 4 Oct 2013 14:42:49 +0000 (10:42 -0400)
committerRicardo Signes <rjbs@cpan.org>
Fri, 4 Oct 2013 14:42:49 +0000 (10:42 -0400)
based on suggestions from Nicholas Clark and David Golden on
perl5-porters

pod/perlfunc.pod

index 80dc367..3783564 100644 (file)
@@ -6831,18 +6831,20 @@ The following equivalences hold (assuming C<< $#a >= $i >> )
     unshift(@a,$x,$y)   splice(@a,0,0,$x,$y)
     $a[$i] = $y         splice(@a,$i,1,$y)
 
-Example, assuming array lengths are passed before arrays:
-
-    sub aeq {  # compare two list values
-        my(@a) = splice(@_,0,shift);
-        my(@b) = splice(@_,0,shift);
-        return 0 unless @a == @b;  # same len?
-        while (@a) {
-            return 0 if pop(@a) ne pop(@b);
-        }
-        return 1;
+C<splice> can be used, for example, to implement n-ary queue processing:
+
+    sub nary_print {
+      my $n = shift;
+      while (my @next_n = splice @_, 0, $n) {
+        say join q{ -- }, @next_n;
+      }
     }
-    if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
+
+    nary_print(3, qw(a b c d e f g h));
+    # prints:
+    #   a -- b -- c
+    #   d -- e -- f
+    #   g -- h
 
 Starting with Perl 5.14, C<splice> can take scalar EXPR, which must hold a
 reference to an unblessed array.  The argument will be dereferenced