If you look at the documentation by running:
perldoc -f split
You will see three forms of arguments that split can accept:
split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/
This means that even when you pass a split string as the first argument, perl forces it to be a regular expression.
If we look at the warnings we get when we try to do something like this in re.pl :
$ my $string_with_backslashes = "Hello\\there\\friend"; Hello\there\friend $ my @arry = split('\\', $string_with_backslashes); Compile error: Trailing \ in regex m/\/ at (eval 287) line 6.
we see that, firstly, '\\' interpolated as a backslash escape, followed by the actual backslash, which is evaluated by a single backslash.
split then puts the backslash that we gave, and forces it to regex, as if we wrote:
$ my @arry = split(/\/, $string_with_backslashes);
which does not work because there is only one backslash that is interpreted as simply escaping the forward slash after it (without the presence of a trailing / ) to show that the regular expression has ended.
dg123 source share