http://qs1969.pair.com?node_id=1136546


in reply to Re^3: split function not working as expected
in thread RE: split function not working as expected[SOLVED]

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = 'first|second|third'; my @ra = split '[|]', $s; dd \@ra; " ["first", "second", "third"]
Also works with  "[|]" and  /[|]/

Update: The reason  "\|" doesn't work is that in double-quote interpolation, the  \ (backslash) acts, in this case, on the  | (pipe) to produce a literal pipe. Double-quotish interpolation would need a doubled backslash to yield a single literal backslash in the compiled string:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = qq{\|}; print qq{:$s:}; ;; my $t = qq{\\|}; print qq{:$t:}; " :|: :\|:
That's why it's generally better to use  qr{pattern} to compile a regex. (I use  qq{...} in the example because Windows command line uses "..." to quote everything and  qq{...} reduces the need for backslashes.) Contrast with  '\|' and  '\\|' Please see Quote and Quote-like Operators and Quote-Like Operators.


Give a man a fish:  <%-(-(-(-<