madtoperl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: split and the null string
by mildside (Friar) on May 16, 2006 at 06:40 UTC
    Is there a question here?

    If I understand your assertion correctly, I disagree with it. The Camel book says 'If LIMIT is negative, it is treated as if an arbitrarily large LIMIT has been supplied'.

    So the last two cases in your example are entirely consistent.

    Cheers,

    mildside

Re: split and the null string
by bart (Canon) on May 16, 2006 at 08:06 UTC
    I agree with you there, I remember working on a script some years ago and this biting me in the ass. In particular, note the difference between split /:/, "A", -1 (1 field) and split /:/, "", -1 (0 fields).

    So, you can consider it a bug — or a bad design desicion. Or, you can feel that this is the proper way to behave. I'm no sure either way. It most definitely is a corner case, and if you want it, it's best to treat a zero length string as a special case, returning (""), and have any other string be delt with by split.

Re: split and the null string
by jwkrahn (Abbot) on May 16, 2006 at 09:27 UTC
    This behavior is described in the documentation for split:

    If LIMIT is specified and positive, it represents the maximum number of fields the EXPR will be split into, though the actual number of fields returned depends on the number of times PATTERN matches within EXPR. If LIMIT is unspecified or zero, trailing null fields are stripped (which potential users of pop would do well to remember). If LIMIT is negative, it is treated as if an arbitrarily large LIMIT had been specified. Note that splitting an EXPR that evaluates to the empty string always returns the empty list, regardless of the LIMIT specified.