Remix.run Logo
js2 3 hours ago

Or even worse:

  my($f) = `fortune`; # assigns first line of output to $f.
  my $f = `fortune`; # assign all output to $f.
Which allegedly got a HS kid in hot water[^1].

[^1]: "It's all about context" (2001): https://archive.ph/IB2kR (http://www.stonehenge.com/merlyn/UnixReview/col38.html)

PunchyHamster 2 hours ago | parent | next [-]

I think what's most likely to happen here is that: * a developer that knew how it worked used it in code where he *wanted* to get the first line * someone just starting up copied it over and assumed that's the way to get the content of command into a variable

It's essentially complaining about using feature wrong on purpose, because the person that made mistake never learned the language.

my($var1, $var2...) is a way to multi-assign variables from an array.

and that makes perfect sense when you look at it. Perl have no multiple returns, but if you need a function that returns 2 variables it is very easy to make it work with:

    my ($bandwidth, $latency) = speedtest($host)

Perl's feature for returning different type depending on caller is definitely a confusing part but

    my @lines = `fortune`
returning lines makes perfect sense for the use case (you call external commands to parse its output, and if you do that you generally want it in lines, because then you can just do

   foreach my $line (`fortune`) {}
and it "just works".

Now you might ask "why make such shortcuts?". Well, one of big mistakes when making Perl is that it was also aimed as replacement for sed/awk for the oneliners, so language is peppered with "clever short ways to do stuff", and it's a pleasure to use in quick ad-hoc oneliners for CLI.... but then people try to use same cleverness in the actual code and it ends up with the unreadable mess people know Perl for.

the fact you can do

    my ($first_line, $second_line, ...) = `fortune`
is just the feature being.... consistent in its use "when you give it array, it will fill it with lines from the executed command"

you gave it array, and it just did what it does with arrays.

weare138 2 hours ago | parent | prev [-]

Then don't use the low level interfaces. In Perl, language features are plug and play. Everything's in a module. Use the core module List::Util instead.