Monday, February 1, 2016

Perl6: error by design in split()?

So while splitting a string, I found some odd results.  But it's actually NOT a bug... and therefore I consider that to be a DESIGN ERROR.

Suppose we have a $string "Hello, world!", and we split it into characters.  In Perl, we do it these ways:

my @foo = split '', $string; # perl 5
my @foo = $string.split(''); # perl 6

And in both cases, we end up with an array:

[ H e l l o ,   w o r l d ! ]

HOWEVER, in Perl6 I see something odd: two extra characters in my stream:

[]  <= what's that?  \0? \b?
[H]
[e]
[l]
[l]
[o]
[,]
[ ]
[w]
[o]
[r]
[l]
[d]
[!]
[] <= what's that?  \0?  \b?


So let's check to see if those two end elements are defined.

for @bar -> $letter
{
   printf "[$letter] %x\n", $letter.ord if $letter;
   say "undef" unless $letter;
}


undef
[H] 48
[e] 65
[l] 6c
[l] 6c
[o] 6f
[,] 2c
[ ] 20
[w] 77
[o] 6f
[r] 72
[l] 6c
[d] 64
[!] 21
undef



Looks like a bug to me.  And yet the docs say:

If DELIMITER is a string, it is searched for literally and not treated as a regex. If DELIMITER is the empty string, it effectively returns all characters of the string separately (plus an empty string at the begin and at the end).

A number of optional named parameters can be specified, which alter the result being returned. The :v, :k, :kv and :p named parameters all perform a special action with regards to the delimiter found.

  • :skip-empty
If specified, do not return empty strings before or after a delimiter.

Baloney.  That's not how I expect split to work BY DEFAULT.  I expect that string to be split, and nothing added, thank you very much.

EVEN BETTER, :skip-empty doesn't appear to work in the admittedly pre-1.0 version of Perl6 I'm running (2015.09).







1 comment:

  1. I'd have an easier time with that if there was some explained reason for the behavior. This way, it just seems... odd.

    ReplyDelete