|
|
|
|
|
by __david__
4546 days ago
|
|
> The two '$_'s in the map block actually refer to two different variables Yeah, but that's Perl 101. Grabbing an element of array @a is $a[0], which is wholly different than plain $a. That's not really a style thing, it's a Perl thing, for better or for worse. > I'm sorry, but I can't get worked up about not being able to write code like that. Why? It looks more or less reasonable to me. Most of the weirdness there is a factor of Perl's unfortunate lack of named function parameters. @_ and $_[] are simply a fact of life when writing Perl. It's even a single expression which means you could probably write a similar one liner in python (since their wimpy "lambda" only does expressions) |
|
That code confused me after working in nothing but Perl for 4 years. It was confusing because I wasn't used to people having an array (@_ in this case) and a scalar ($_) named the same thing, and used in close approximation. The issue could have been avoided by (e.g.) setting $_[2] to a variable first, and would have made the code more readable. It's not "Perl 101" to write code that is intentionally obtuse.
> Why? It looks more or less reasonable to me. Most of the weirdness there is a factor of Perl's unfortunate lack of named function parameters. @_ and $_[] are simply a fact of life when writing Perl.
That code could be written in a way that was easier to read and maintain. For example, what is 'shift' intended to be? The only information we have is that it's supposed to be the first argument to function1.
Edit:
> Most of the weirdness there is a factor of Perl's unfortunate lack of named function parameters
This was in a code base with a source filter to provide function parameters. That could have been written as:
in that code base, but the developer in question chose not to.Even without said source filter, you can still name the function parameters: