Explaining why to use $array[0] instead of @array[0]
[email protected] (DavidB) Tue, 21 Oct 2008 13:32:02 -0700 (PDT)
| Newsgroups | perl.trainers |
|---|---|
| Organization | http://groups.google.com |
| Message-ID | <cf58536d-7e61-4354-b396-6fcdcf32203b@u57g2000hsf.googlegroups.com> |
Hi, I can think of some reasons we use $ instead of @ for array indices (in Perl5): 1. because we are referring to a scalar, not an array slice, and it's misleading to use @ 2. because it's common usage and all professional code uses it 3. because @array[0] may in some selected cases be treated as a list, for example @array[0] = @array2 (copies first element) vs. $array[0] = @array2 (copies number of elements) however, aside from the extremely limited-application code example in 3. above, I was unable to come up with a practical justification for using @. I'm talking demonstrable reason, not any "just because it's proper" reasons like 1 and 2 above. I can talk their ears off telling them it's the right thing to do, but I'd prefer to have some practical reason. thanks! David