in reply to Array of variables

Indeed, if there is but one fragment of Perl-voodoo that you need to read about right away, it would be:   references.   (See: perldoc perlref, perldoc perlreftut, and their many brethren.)

The essential idea is a disarmingly simple one:   a “reference” is “a single, scalar thing” (so it qualifies to be the value of a variable, or an element in an array or in a hash ...) which can “refer to” anything-at-all.   Perl takes care of all the messy memory-allocation, reference-counting and garbage-collection headaches for you, and for the most part, it is also quite prescient when it comes to understanding the code that you’ve written.   (Perl was written by people who need to get work done, not by computer academics.)

You can pretty much build “arrays of hashes of hashes of arrays of ...” with impunity, because you actually build “arrays of references to hashes of references to hashes of references to arrays of ...”   (This is also where the Perl slang terms, “arrayref” (== reference to array) and “hashref” (== reference to hash) come from.)

The Perl language also allows you several different ways to write the same thing ... the choice being up to you.   Unlike most other languages, Perl does not impose “the designer’s one-and-only opinion as to what is ‘the right way to do it’ ” upon you.   This philosophy gives rise to TMTOWTDI == “Tim Toady” == There’s More Than One Way To Do It.™”

Replies are listed 'Best First'.
Re^2: Array of variables
by chromatic (Archbishop) on May 28, 2013 at 21:40 UTC

    I'm sorry, but I downvoted this because I think its really unhelpful. Parts of it are wrong and other parts are misleading—but I'm also getting a headache trying to read it.

    Could you please elide the random typography? I feel like you're trying to communicate something but it's getting lost along the way.

      Are you seriously criticizing him for using proper typography? We’re living in the 21st century, for Larry’s sake. Unicode is everywhere; we no longer have to use typewriter quotes or ASCII equivalences (e.g. ... instead of …).

      Could you please elaborate on what parts of the post you think are wrong or misleading?

        Are you seriously criticizing him for using proper typography?

        No; I used the word "random" with deliberation.

        Could you please elaborate on what parts of the post you think are wrong or misleading?

        The idea that the OP needs to read about references right away gives an urgency I think is misleading. The OP's question seems more about Perl's notion of container and value semantics than references.

        The idea that a reference can refer to the value of a variable is technically true, but I think that's also misleading, again due to Perl's notion of container and value semantics.

        Perl's garbage collection mechanism does not rely on references at the language level. Perl's memory management occurs at the SV level, which is how an RV participates in memory management. That distinction is probably irrelevant at every level for the OP, but getting the details of that backwards seems seriously misleading.

        The idea that Perl goes to great lengths to interpret what you mean by references is dangerously misleading; it's true in certain cases of autovivification, but if you look at Perl 5.16 and the mess that is sigilless each, you'll see that taking that idea too far is out of character of the rest of the language. Polymorphic operators are a mistake with smartmatch and they're a mistake with sigilless aggregate operators too; you don't even have typeful semantic operator hints as a reader of the code.

        Claiming that the terms "arrayref" and "hashref" refer to nested data structures is misleading. I think sundialsvc didn't intend to claim this, but I admit I don't understand what he was trying to say there.