On 17 February 2013 02:56, Tom Christiansen <tchrist@perl.com> 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. so: 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; or 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. cheers, Yves -- perl -Mre=debug -e "/just|another|perl|hacker/"Thread Previous | Thread Next