I'm the author of pythonizer, perl to python converter, and I'm trying to translate a perl split statement that has a string pattern that includes a backslash, and I need some help understanding the behavior. Here is the example based on the source code I'm trying to translate:
$s = 'a|b|c';
@a = split '\|', $s;
print scalar(@a) . "\n";
print "@a\n";
The output is:
3
a b c
Now if I just print '\|' it prints \| so I'm not sure why the backslash is being ignored in the string pattern. The documentation doesn't say much of anything about a string being used as a pattern, except for the ' ' special case. Feeding '\|' to python string split will not split this string.
Even more strange is what happens if I change the above code to use a double-quoted string:
@a = split "\|", $s;
Then the output is:
5
a | b | c
If I change it to a regex, then it does the same thing as if it was a single-quoted string (splitting into 3 pieces), which makes perfect sense because | is a special char in a regex so it needs to be escaped:
@a = split /\|/, $s;
So my question is - how is a split on a string that contains a backslash (in single and then double quotes) supposed to work so I can reproduce it in python? Should I just remove all backslashes, except for \\ from a single-quoted input string if it's on a split?
Also, why does a split on "\|" (or "|") split the string into 5 pieces? (I'm thinking of punting on this case.)