develooper Front page | perl.perl5.porters | Postings from February 2013

Re: [perl #116086] split "\x20" doesn't work as documented

Thread Previous | Thread Next
February 24, 2013 15:58
Re: [perl #116086] split "\x20" doesn't work as documented
Message ID:
On 17 February 2013 02:56, Tom Christiansen <> wrote:
> I've seen people try to do this:
>     my $delim = $if_something ? " " : qr/\s+/;
>     @fields = split $delim, $string;
> And they are *very* surprised that it doesn't work the same as
>     @fields = split $if_something ? " " : /\s+/, $string;

Well I think you got the example wrong. Those two actually are the
same, but if you replaced $if_something with a constant, then it would
be different, and agreed surprising.

The rule is that if the argument to split can be resolved at compile
time to a string containing  a single space then the special behavior
takes place.


use constant IF_SOMETHING => 1;
@fields = split IF_SOMETHING ? " " : /\s+/, $string;

behaves the same as:

@fields = split " ", $string;

not like:

@fields = split $if_something ? " " : /\s+/, $string;


my $delim = $if_something ? " " : qr/\s+/;
@fields = split $delim, $string;

Which I consider to be a bug. The special behavior should cut in if
the argument to split is a string and not a regexp. But I havent
figured out how to make this work properly.

> That is really hard to explain, you know?  Not easy to justify either.

Yeah, I agree. It shouldn't work like this.

> Either it should clearly state that this space trick is a magic literal
> literal and that cannot be in variable, or else it should be fixed so
> anything that shows up as a U+0020 counts as that.

We could document how it does work better for now. Id like to get it
fixed however so that the rule was that if the split argument was not
a qr// and not a bare m/.../ that it would get the magic behavior. I
havent figured out how to make that work yet tho.

IOW i believe that anything that resolves to a string eq to " " should
get the special behavior. Anything that is qr// or // quoted should
not. Regardless as to whether it is compile time.


perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About