logoalt Hacker News

atherton94027yesterday at 6:20 PM6 repliesview on HN

There was so much complexity hidden behind "do what I mean". For example, scalar vs array context which was super subtle:

  my @var = @array  # copy the array
  my $var = @array  # return the count of elements in array

Replies

js2yesterday at 6:26 PM

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)

show 2 replies
wlonklyyesterday at 11:55 PM

It's funny, because something like

    array var = array  # copied
    int var = array    # length
or

    var = array  # copied
    var = array.to_i  # length
is less subtle to me but I can't put my finger on why.
totallykvotheyesterday at 7:17 PM

That's not super subtle any more than it's super subtle that "*" performs multiplication and "+" performs addition. Sometimes you just need to learn the language.

This is not a general defense of Perl, which is many times absolutely unreadable, but this example is perfectly comprehensible if you actually are trying to write Perl and not superimpose some other language on it.*

show 1 reply
creeryesterday at 7:03 PM

What exactly is complex or "super subtle" about this? It's the textbook example from the 1st chapter in the tutorial or something?

show 1 reply
agumonkeyyesterday at 7:17 PM

I always found contextual eval interesting. It's a generalized version of toString in a way

petreyesterday at 8:12 PM

It's not complexity, it's magic. Useful when one cannot be bothered to write array.length. So is if (@a) when the array is empty.