- Foreach loops alias $_ to each element of the list in turn (unless told to use a different variable). Any change to $_ will change the element to which its aliased.
- and short-circuits, meaning it will only evaluate its RHS if its LHS is true.
- defined with no argument is defined($_).
- @$row is an array dereference. It means "the array referenced by $row".
These all do the same:
!defined and $_ = ' ' for @$row;
for (@$row) {
!defined and $_ = ' ';
}
for (@$row) {
if (!defined) {
$_ = ' ';
}
}
for $_ (@$row) {
if (!defined($_) {
$_ = ' ';
}
}
for my $i (0..$#$row) {
if (!defined($row->[$i]) {
$row->[$i] = ' ';
}
}
In 5.10 and above, that could have been written
$_ //= '' for @$row;
| [reply] [d/l] [select] |
not defined( $_ ) and $_ = ' ' for @$row;
Or more simply in modern versions of Perl:
$_ //= ' ' for @$row;
1) ! means "not" as in the not operator (see example above.)
2) You can also use it for filehandles '_' and subroutines '&_'. It is just another valid character that can be used as variable names (see perlvar.)
3) $row contains a reference to an array and to dereference the array you put a '@' in front of $row.
| [reply] [d/l] [select] |
$ cat junk.pl
! defined and $_ = ' ' for @$row;
$ perl -MO=Deparse,-p junk.pl
;
(defined($_) or ($_ = ' ')) foreach (@$row);
junk.pl syntax OK
I've never understood what this means '$_',
Then you haven't read perlintro, go read it now, and try out the examples.
Also see perlvar, it also explains $_
In other words, what does @$row mean or what is it accomplishing?
It is de-referencing, see references quick reference, basically
my @array = 1 ..2;
print "@array\n";
my $ref = \@array;
print "@$ref\n";
__END__
1 2
1 2
| [reply] [d/l] [select] |
Unfortunately for you, you’ve stumbled into a bit of “Perl golf.” Such code is easy-to-read for the initiated, but quite confusing otherwise. I routinely re-write such code, or at least add comments directly above it. Your questions are entirely reasonable... because the code, as written, doesn’t expressly tell you.
Perl has many “predefined variables,” and it allows you to write code that omits any explicit reference to any variable, in which case the code is understood by Perl to implicitly reference certain of those variables. One of those variables is $_, which is, basically, “the last thing that was read or referenced.” (See the perldoc topics previously referred-to in other replies.)
Further complicating the situation is Perl’s use of “contexts.” In other words, it means something when you use $ vs. @ vs. %, and this meaning is sometimes quite arcane unintuitive.
Mind you, “I come neither to praise Caesar, nor to bury him,” but merely to describe him. My comments here are merely meant to be “factual, descriptive, and neutral.” You will encounter code like this, and in time, you may find yourself writing code like this, and “so be it.” Certain aspects of the Perl language rely upon knowledge of the subtleties of the language ... subtleties which are, some might argue (and some might dispute) are the language’s greatest strengths. It does bring you a bit of a learning curve that you might not have expected, and a bit more of a learning curve than you might have wished for. But these things, too, shall pass.
Welcome to the Monastery, and to the Perl community. Come here often. Ask away.
| |
Well, to the uninitiated, everything is magic
| [reply] |