In this code snippet:
use strict;
use warnings;
use Data::Dumper;
my $r = [qw(testing this thing)];
print Dumper($r);
foreach my $row (@({$r})
{
print "$row\n";
$row .= 'mod';
}
print Dumper($r);
print Dumper(@({$r});
I figured out that the ‘(‘ after the ‘@‘ in the foreach is causing this not to loop correctly. I have no idea why this code even works as there is no ending parenthesis. What is this doing? It looks to be creating a new variable on the fly, but shouldn’t ‘use strict‘ have fired or something?
Please help explain what that ‘@(‘ is doing and why it still runs without an ending parenthesis.
That is a hash slice of the
%(variable, which being part of the*(glob, is exempt from strict vars. This is true for variables that Perl has predefined, in this case$(and also for all of the other glob slots for the names of punctuation variables. All punctuation variables are global across all packages, and their fully qualified names are the short forms:$),@),%),&)… Sincestrict 'vars'does not apply to fully qualified names, none of these names are errors.Expanding a bit:
Those lines are all equivalent.
With
use warnings;perl will let you know that it would be better to write a slice of a single value with a$sigil:Which in trying to resolve a typo would have pointed you in the right spot. Which is why you should always use both warnings and strictures.
For more detail, the perlvar manpage shows all of the punctuation variables with at least one sigil or another. And if you wanted a reference about the scoping of punctuation variables, the package docs have that.
All the punctuation variables are also immune to
used only oncewarnings, and that might be a bug…