2

Consider the following Perl script:

#!/usr/bin/perl

foreach(split '$$', 'A$$B'){ print; }
print "\n";
foreach(split '\$\$', 'A$$B'){ print; }
print "\n";

Given the well-known fact that Perl performs variable interpolation in double-quoted strings only, the $$ in the single-quoted strings above is expected to be treated as a literal $$ rather than the PID of the current process. Thus, the expected output of the script is

AB
AB

However, when running this script with both Perl v5.34.1 and an online Perl compiler, the following is outputted instead:

A$$B
AB

This seems to imply that when using '$$' in split, the $$ is getting interpolated, contrary to the specification that single quotes perform do not perform variable interpolation.

This unexpected result cost me considerable time debugging an otherwise-functional Perl program. While I succeeded in resolving the issue by escaping the dollar signs in the single-quoted string used for split, I am still curious why this behavior occurred, and I was unable to find any sources suggesting it is expected.

Is this a bug in Perl, or are there scenarios where interpolation into a single-quoted string is expected to occur?

2
  • 2
    The first argument to split is a regular expression pattern, and of course $ has a special meaning with a regex. Does that explain what you're seeing? Commented Jul 31 at 3:24
  • @pilcrow - I mistakenly believed split took a regex or string, since split accepts a string argument, even with use strict; and use warnings;. If you write your comment as an answer, I will accept it. Commented Jul 31 at 3:28

1 Answer 1

8

The first argument to split is a regular expression pattern, even if single-quoted. So, your first case has two end-of-line anchors whereas the second splits on two dollar signs.

Update: ikegami rightly clarifies that split has a special case when the pattern is a single space character string. In this case it works like awk, trimming leading whitespace and splitting on /\s+/.

Sign up to request clarification or add additional context in comments.

4 Comments

Nit: Re "first argument to split is a regular expression pattern", Unless it's a single space provided as a string.
the other split special case (though still a regex) is '^' or /^/ which is assumed to be /^/m because it would be useless otherwise
(and also discards the leading empty "")
"In this case it works like awk" - just wanna clarify on this on the awk side - split(str, arr, " ") in awk trims leading + trailing runs of /[ \t\n]+/ then split cells using that same regex - it's a strict subset of ASCII /[[:space:]]/, omitting \v \f \r. I'm only speaking about the awk side of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.