develooper Front page | perl.trainers | Postings from October 2008

Explaining why to use $array[0] instead of @array[0]

Thread Next
From:
DavidB
Date:
October 21, 2008 15:24
Subject:
Explaining why to use $array[0] instead of @array[0]
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



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About