Interpolating Array Values
Array variables are interpolated into double-quoted strings by joining
all elements of the array with the separator specified in the
$" variable[51] (which contains a space by default). The following are
equivalent:
my $temp = join( $", @ARGV ); print $temp; print "@ARGV";
Within search patterns, which also undergo double-quotish
interpolation, there is an unfortunate ambiguity: is /$foo[bar]/ to be interpreted as /${foo}[bar]/ (where [bar] is a character class for the regular
expression), or as /${foo[bar]}/
(where [bar] is the subscript to
array @foo)? If @foo doesn’t otherwise exist, it’s
obviously a character class. If @foo exists, Perl takes a good guess about
[bar] and is almost always
right.[52] If it does guess wrong, or if you’re just plain
paranoid, you can force the correct interpretation with braces as
shown earlier. Even if you’re merely prudent, it’s probably not a
bad idea.
[51] $LIST_SEPARATOR if you
use the English module bundled with Perl.
[52] The guesser is too boring to describe in full, but
basically takes a weighted average of all the things that look
like character classes (a–z,
\w, initial ^) versus things that look like
expressions (variables or reserved words).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access