
Martin
blogs about
how the Camel book mentions the fact that scalars, arrays, and hashes
have completely separate namespaces, as if this is a problem. Well, I
disagree.
As with anything in Perl: yes, obviously it's possible to confuse
yourself with this feature, and yes, obviously it's true that if you do
this, you will get to keep both pieces of your program. So don't do that
then.
However, when used properly, I find that it actually helps me
improve readability of my programs.
Let's say we have a subroutine which returns a reference to a
filehandle to a bunch of data in a format that we know how to parse. We
don't know what the filehandle points to (and we don't care, either),
but we do know what the data is. So we store the filehandle in some
scalar, and then proceed to parse the data into a hash.
Now we
could use variable names such as
$fh and
%hash, but I'll hope that everyone agrees with me that this is
very bad practice. So, instead, we should name the variables
after what they contain. Say our data file contains books:
$books = get_books_fh();
while(<$books>)
(... parse $author out of $_ ...)
push @ $books $author , $_;
The filehandle to which
$books is a reference, points to
some useful data; and the
%books hash table contains the
exact same data, only structured in a different, more useful
way. So why give them a different name? They're the same thing, call
them the same way. Personally, I find this makes a whole lot of
sense. And you can do the same thing with arrays vs hashes, or scalars
vs arrays.
Of course you can also write code that uses
$somevar
and @somevar, with no semantic connection between the
two. That is stupid, so don't do it. The fact that you
can do
stupid things with Perl, however, should not mean the language
sucks.
You see, other than
some other
programming language I could think about, Perl does
not
assume its users are stupid, or inexperienced, or dummies, or whatever.
Yes, that does mean that new users will get confused; but it also allows
experienced users to Get Stuff Done in a proper way. And, well, I'm sure
you can do stupid things with python, too.