The reason that an array subscript is "$foo[0]" is because the $ sigil means "HEY COMPILER, the expression here is going to evaluate to some scalar".
@foo is the entire list and the compiler treats it as a list type; $foo[0] is an element of the list and needs storage/behavior of a scalar. Same thing for hashes: %bar is the whole hash; $bar{quux} is an element and must be treated by the compiler as a scalar.
The programmer must remember that square braces mean array subscript and curly braces mean hash lookup.
So basically, this is a compiler optimization implemented by having the programmer provide hints to the compiler. It's overhead that probably isn't needed in the modern age, but we're effectively stuck with it. It wouldn't be so bad if they hadn't made the second (and IMO worse) decision:
You can reuse symbols across contexts. The way this works is that the compiler maintains a symbol table where each symbol has a slot available for each of the types (scalar $, array @, hash %, subroutine &). This was originally the way to emulate pass-by-reference: you'd write a subroutine that assigned its arguments into typeglobs (* foo — think of it as a wildcard for all things named foo that behaves as a magic scalar with the contents of foo's symbol table entry) and then pulled them back out as the types it wanted:
local(*foo) = @_;
foreach $bar (@foo) {
do_something($bar);
}
This amounts to telling the compiler "I want to alias the name foo in all contexts to my argument, and then go look at what's stored in the array at that name" and is a poor man's pass-by-reference.Perl 5 has a real reference system that completely obviates the need for this, except for the case of monkey-patching a subroutine, where you still say:
local *Package::quux = sub { ... }"
What's left is an unfortunate case where things like the GP mentioned ($bar = $foo[$foo]) are possible, and people who think they're being clever will do these things. Like the parent said, this is not a good thing to do.I'm not entirely sure that's correct. I think Larry Wall (being a linguist originally) designed it that way because he thought that using context-sensitive sigils was more like regular speech where leading words indicate the number, i.e. 'a cup' vs. 'some cups'...
Having taken a class on linguistics, I have to say basing a computer language on it is a horrible idea.
Human languages are far more complex and verbose than is required for talking to computers. Consider that it takes years to master a spoken language. A computer language should be much easier to pick up once you already know how to use an existing language. Computer languages should attempt to reduce complexity and verbosity where possible.
Example: I picked up lua in a week. (javascript and ruby experience helped a lot) I'm not an expert by any means but I'm capable of writing usable applications. I doubt a person could pick up a new human language in a week.
This is mostly a quibble, but human languages are not more verbose than computer languages, generally. I can generally rely on you to allocate all the variables and present the result in a context-aware way when I ask you, "What is 3 + 5?" I'd generally have to allocate variables and tell the computer where to put the result if I were to ask it the same question.
What was, and still is, sort of magical about Perl is the degree to which it is aware of context and can use it to sort out meaning. If anything, the chief complaints against Perl, which stems from its similarity to natural languages (!), is its terseness and expressive power, these complaints being that it's indistinguishable from line noise and is a write-only language.
https://github.com/schwern/AAAAAAA/blob/aaaaaa/aaa/AAAAAAAAA...
It should be unparsable, but most people who have some Perl knowledge find they can actually read and understand this.
It's quite difficult to get people to use your new version when you actively tell them that it's probably not a good idea right now.
use v5.10;
use strict;
use warnings;
my $ref = ['one', 2, 3];
say $ref;
foreach (@{$ref}) {
say;
}
# These are the same. The first form is much cleaner.
# It also makes more sense semanticaly
say @{$ref}->[0];
say @{$ref}[0];
say $ref->[0]; say @{$ref}->[0];
This raises a deprecation warning for me. say @{$ref}[0];
This is technically correct, as the leading sigil "@" indicates you are returning an array, but since you're only returning one element in that array, this should probably be "${$ref}[0]" which returns a scalar only. say $ref->[0];
This the only form which I would consider correct or "clean".This example proves nothing about Perl's power, though. The fact that difficult and fiddly reference handling shenanigans are necessary to handle something as trivial as nested arrays and hashes shows quite the opposite.
It's perfectly fine to use the array sigil, in which case it's a one-element list instead of a scalar. That's useful sometimes.
Compared to what? C? In what way is it more powerful than Ruby, Python, Ocaml, Haskell, etc.?
There are things perl has over e.g. python (lambdas) but there are at least as many things those languages have over perl (e.g. python generators built in) and all of those languages are easier to use. Look at the code you wrote there! I assume "use v5.10" is actually a forward compatibility declaration instead of a backward one, right? Really embarrassing if so.
The most feature-complete implementation - Rakudo on Parrot - is not production-ready, though, and currently somewhat stalled due to low bus factor: The object-system refactor (which made things like natively-typed attributes possible) introduced a lot of regressions, and the regex engine in particular is not yet fixed as the lead-developer was hit by real-life issues.
That's a little disingenuous. I'm a Perl fan and I'd like to use Perl 6 practically, but the fact that someone wrote a Tetris clone in one of multiple incomplete implementations of Perl 6 doesn't mean that Perl 6 is actually useful for much.
Not really. It just shows that feature-completeness may be a good metric when comparing implementations of the same language, but not so much when comparing them to implementations of a different language.
the fact that someone wrote a Tetris clone in one of multiple incomplete implementations of Perl 6 doesn't mean that Perl 6 is actually useful for much
The fact that there's a Tetris-clone in Perl6 indeed doesn't mean much. However, the fact that it uses GTK is an example of CLR interop, which opens up a whole new level of practical applicability.
In the company I worked for that used Perl extensively, there were coding standards. In fact, you don't even need that, you just need to remind everyone that cleverness is not a goal in writing maintainable software, even if cleverness is what Perl's best at.