talexb has asked for the wisdom of the Perl Monks concerning the following question:
I've been part of this community for a while, and it's always fun to check out Selected Best Nodes for pearls (heh) of wisdom. Today I found Being pretentious, and getting away with it., which talked about the behaviour of split with a particular regex.
I started writing a module to beautify code recently, and ended up using a capture in a split statement -- which isn't something I do regularly. Here's the interesting part about this subject (finally) .. the example in the node talked about the following behaviour:
-
In Perl 5.005_02, split /(A)|B/, "1A2B3" returned a five-element list of (1, 'A', 2, undef, 3). In 5.005_03, it returned (1, 'A', 2, '', 3); a subtle, but meaningful, difference. There's only one way to get an undef from split(), and that's from the underlying regex match. A capturing paren that does not match has undef as its $DIGIT value.
-
A tiny bit of incorrectly written code in pp.c:pp_split() caused these undefs to become empty strings. Bah. I corrected it, documented it, and tested it.
Oh.DB<2> @foo = split /(A)|B/, "1A2B3" DB<3> x @foo 0 1 1 'A' 2 2 3 undef 4 3 DB<4>
The behaviour has gone back to the previous one. I believe it's because split is splitting on the un-captured 'B', and since it's not captured, undef is the correct result, rather than a null string (with apologies to japhy.
Thoughts?
EDIT: Updated title to add '/ using a capture during split' to the title, as per feedback.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re-reading history from 2001
by vr (Curate) on Mar 05, 2019 at 16:56 UTC | |
by choroba (Cardinal) on Mar 05, 2019 at 17:14 UTC |