I'm re-acquainting myself with Perl, and have just used module-starter
to initialise a new project. I'm now trying to understand the generated code. All is fine apart from the follow line indicated :
sub not_in_file_ok {
my ($filename, %regex) = @_;
open( my $fh, '<', $filename )
or die "couldn't open $filename for reading: $!";
my %violated;
while (my $line = <$fh>) {
while (my ($desc, $regex) = each %regex) {
if ($line =~ $regex) {
##I'm having problems here
push @{$violated{$desc}||=[]}, $.;
}
}
}
...
}
I have two problems:
- The
||=[]
. Is this|
followed by|=
, or is this an or||
followed by an=[]
. Can someone talk me through what is happening here? (I'm guessing "if the hash is empty the create an empty anonymous array to initialise the hash", but I'm struggling to see how that is formed from the code.) push @{$violated{$desc}}, $.
I understand this to mean "assign the line number to the key$desc
for the hash%violated
. But from the code I read, "lookup the value of the keydesc
of$violated{$desc}
(the$violated{$desc}
part), then use this value as a symbolic reference to an array (the@{$value}
part), then push the line number onto that array". I don't see how to reconcile these two views.
I think there is a lot for me to learn in this line of code - can someone help me by walking me through it?