I am new to Perl and currently trying to debug a Perl script.
I encountered a line which I have no clue what it is trying to do.
Hope some experts here can help me out.
grep { ${ $_ }[0] =~ /^$ptype/ } @ports
You don't need an expert. You just need a clearer understanding of the
pieces of the language involved:
grep { EXPR } LIST
returns all those elements of LIST (@ports, in this case) for which
EXPR is true, setting each element of LIST to $_ in EXPR
EXPR =~ /^$ptype/
returns true if the string contained in EXPR contains the substring
"beginning of string, followed by the contents of $ptype"
${ REF }[0]
is the first element of the array referenced by REF
$_
as we already said, will be aliased to each element of @ports
Putting it all together:
Loop through each member of @ports. For each member, look at the first
element of the array that member references. If that element contains
the variable $ptype at it's start, add this member of @ports to the
list of values returned by the entire expression.
For more information:
perldoc -f grep
perldoc perlref
perldoc perlre
Hope this helps,
Paul Lalli