in reply to Mystery interaction between split and gobbling arrays

Tip #6 from the Basic debugging checklist... B::Deparse

use strict 'refs'; my $str = 'foo:bar:'; my($a, @options) = split(/:/, $str, 0); my($b, $c, $d) = split(/:/, $str, 4); use Data::Dumper; print Dumper([$a, \@options]); print Dumper([$b, $c, $d]);

Notice how the 2 split lines have different LIMIT values (0 and 4).

when assigning to a list, if LIMIT is omitted (or zero), then LIMIT is treated as though it were one larger than the number of variables in the list

Replies are listed 'Best First'.
Re^2: Mystery interaction between split and gobbling arrays
by saintmike (Vicar) on Jun 17, 2015 at 23:12 UTC
    Wow, nice find, thanks for that. I'm pretty sure that setting the limit to 4 in the first case saves some cpu cycles, but setting it to 0 in the second case is pretty crazy, given the special behavior of split() in this case.

    Here's one more: What's the difference between

    ( $a, $b, $c ) = split /:/, $str;
    and
    ( $a, $b, $c, @options ) = split /:/, $str;
    when $str = "foo:bar:"?

    Spoiler: Adding @options to the left hand side makes $c undef'd!