in reply to Only using scalers and references

you initiate the hash with %, and add keys with $, which in my book is wickedly confusing.

You're thinking of the big picture while you should be looking at the smaller picture. The sigil is not based on the data structure, it's based on the value being being fetch/set. Hash values are scalars, so "$" is used when fetching/setting hash values.

%hash = ();

I'm surprised you've ever have to do that. Hashes and arrays start off empty. Do you properly scope your hashes and arrays using my? use strict; will identify your most blatant scoping errors (i.e. where you didn't limit the scope at all).

is it ok to program exclusively like this?

Anything abnormal increases the chance of errors and reduces readability.

Replies are listed 'Best First'.
Re^2: Only using scalers and references
by stevesnz (Initiate) on Sep 25, 2008 at 06:02 UTC
    >> Hash values are scalars

    yeah, thats what my understanding is and all the more reason to use references exclusively. cos (correct me if i'm wrong) references are the simplest way to get a hash of hashes

    and (again correct me if i'm wrong) you can't pass a hash or array into a sub - it get 'flattened' into a scaler ... though if you pass a reference of a hash in there it survives perfectly intact

    to me it seems like 'original 1987' perl is somewhat hampered and the the reference arrow (->) was created so that perl can do all the stuff that most modern languages do by default

      the reference arrow (->) was created so that perl can do all the stuff that most modern languages do by default

      First, I think the ability to take references and to dereference them has existed for longer than the "reference arrow".

      Second, references have existed in Perl at least since 1994, before Java and JavaScript even existed.

      Finally, you seem to be confusing references (pointers on which arithmetic cannot be performed) and passing by reference (changing a parameter in a function changes it in the caller). Perl always passes by reference.

      that most modern languages do by default

      What "most modern languages" do is a bad thing. It's a compromise to increase program speed. Perl6 has many improvements in that area.


      The rest of this post is a getting off-topic. Just some nits.

      it get 'flattened' into a scaler ...

      No, it gets 'flattened' into a list.   See perlsub for details on what happens with subroutine arguments.

      Exactly. And the french language is hampered as well, with their subject before verb method of specifiying things ;-).

      Seriously, the use of the sigils in perl may be something that is controversial to newcomers (like the spaces-are-syntax feature of python) and there may be reasons that that approach is not the best, but you are looking at it with PHP eyes. And finding fault in places that are just unfamiliar to you

      I get the same feeling when I look at (((((lisp))))) and if I had to program in lisp today, it would probably be a perl program in lisps clothing. And the same phenomenon happens when c or pascal programmers had to adopt c++ years ago and really only programmed c or pascal in the new language.

      Using references exclusively because they are needed for a hash of hashes is like saying "I eat rice exclusively because without it you can't make nigiri sushi". Sounds like flame bait to me. Or do you really use only one data structure for all problems ?

      Naturally 1987 perl was hampered. It was one of the first of the fifth generation languages and had to invent many of the things the newer languages could later do from the start. If it hadn't evolved it would be history by now. For example object oriented features were build in so that perl could do stuff that most oo-languages do by default. The interesting thing is how seamlessly that was achieved in perl without breaking old stuff.