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

It handles lists that way because of the braindead idea of sigils which, in order to be semantically consistent, cannot have as an element of a list being a list.


I'd like to see you back that up. $a[0][0] does exactly what it looks like it should do: it takes the first element of the list at $a[0]. (Incidentally, this is exactly the same as the gp's $a[0]->[0].) The only problem sigils introduce is the -> in expressions like $a->[0]. ($a is a reference to a list, and you can't write $a[0] in this case because it will look at the list @a.)

I'm not entirely sure why perl doesn't have good support for nested arrays and hashes, but I'm pretty sure it's not to do with sigils.


It's historic. In The Beginning...Perl was a different language, a simpler language built to take the place of awk/sed/grep/shell in one mostly consistent package, and when it later obtained complex data structures, they were implemented on top of the existing semantics, which involved arrays and hashes that could only store scalars.

I don't think anyone thinks list-flattening is a good idea, these days. And, of course, Perl 6 gets better handling of complex data structures...if I recall correctly, it gets an explicit "slurpy" sigil that provides the flattening behavior, but I don't actually work in Perl 6 yet, so I'm not sure of that.


It has nothing to do with sigils. It has to do with the legacy of arrays and hashes only storing scalars in ancient Perl.

Perl 6 does not flatten by default. (And, obviously, sigils remain a core part of the language.


Short answer: Don't write $a->[0]->[0]->[0]->[0]->[0], write $a->[0][0][0][0][0]. It does the same thing.

Long answer: As for writing code, however, you don't need to write $a->[0]->[0]->[0]->[0]->[0]. After the first dereference (if any), you can omit the '->'s because arrays and hashes only store scalars so perl knows you're talking about a reference. You can use all the arrows for backwards compatibility.


The legacy of arrays and hashes only storing scalars is pretty much because of sigils.


Citation needed.




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

Search: