Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

First, I agree that using Perl today feels like using something from a different era. Having sigils at all feels so antiquated to me now.

But the mechanics of the sigil thing never confused me. Either because it was explained to me very well, or possibly -- Larry Wall is a linguist, after all -- because there's an analogy to grammatical cases, which English largely doesn't have anymore, but my native language does.

The sigil depends on what you want to get out of the expression, not what you put in. So @foo would name the whole array, while $foo[2] would name a single element from it.

> If you just need to go one level in you can do either $foo[2] or $foo->[2] depending on your whims. It's kind of madness.

You misremember, but I don't blame you, it's a very unorthodox rule. These are very different expressions.

$foo[2] accesses the second (third depending on how you count) element of @foo. @foo is a "value array", whenever possible, I tended to use those because I found them more convenient and intuitive.

For programmers coming from JavaScript or Python, and people building more complex data structures, using a reference array might be more intuitive. Those are stored in scalar variables, like $foo, but to disambiguate with the syntax for accessing @foo, you have to explicitly dereference it with $foo->[2].



Ah ok I think I remember now:

  my @foo = (1,2);
  $foo[1]; #2

  my $foo_r = \@foo
  $foo_r->[1]; #2

  my @bar = (1,2,$foo_r);
  $bar[2]->[1]; #2
Re the sigils, you're right, I would just add, $ is for scalar and a scalar iirc can be /either/ a value or a reference, that's why $foo_r and $foo[1] above both start with $. That's what I found unhelpful, it would have been nice to have a distinct sigil for refs (but presumably Larry was out of available symbols at that point!).


> there's an analogy to grammatical cases

Best exemplified by Lingua Romana Perligata:

https://metacpan.org/dist/Lingua-Romana-Perligata/view/lib/L...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: