in reply to Re: Control Structure problem, mistake can't be found
in thread Control Structure problem, mistake can't be found

Just wondering, this syntax:
my @quiz = ( {ask => "Name the definitive rock band..", answer => "The Rolling Stones" }, {ask => "What is their best song?", answer => "Moonlight Mile" }, {ask => "What is their best album?", answer => "Sticky Fingers" } );
it's object oriented Perl, right? Or is it just another way to give hash keys a value? I am only familiar with this type of syntax for the same thing:
$myquiz{"question_here"} = "answer here";
I've just never seen it done in this way, and I was wondering what the difference was (which when I looked it up, I thought I understood it to be "object oriented"), and if there is a benefit of chosing one over the other?

Replies are listed 'Best First'.
Re^3: Control Structure problem, mistake can't be found
by GrandFather (Saint) on Aug 26, 2008 at 00:12 UTC

    Let's take it a little bit at a time:

    my @quiz = (...);

    initializes an array from a list.

    {...}

    generates a reference to a hash. A reference is a "handle" that you can use to refer to something else. So:

    my @quiz = ({...}, {...}, {...});

    generates an array containing references to three hashes. Each hash is of the form:

    {ask => "...", answer => "..."}

    The => is the "fat comma". It's exactly like an ordinary comma, except that it pretends the (non-white space) sequence of characters to its left has quotes around it. ask => "..." is the same as 'ask', "...". The fat comma is a handy way of generating a list to initialize a hash and is often used as a heads up to indicate that two items in a list are a pair.

    The intent is to generate a data structure comprised of an array containing a list of hashes where each hash represents a question and answer pair. Other information could be added to each hash such as a weighting for the question (so harder questions are worth more) for example:

    my @quiz = ( {ask => "Name the definitive rock band..", answer => "The Rolling Stones", value => 10, }, {ask => "What is their best song?", answer => "Moonlight Mile" value => 20, }, {ask => "What is their best album?", answer => "Sticky Fingers" value => 5, }, );

    Perl reduces RSI - it saves typing
Re^3: Control Structure problem, mistake can't be found
by FunkyMonk (Bishop) on Aug 25, 2008 at 22:39 UTC
    No, it's not OO perl. It's just declaring an array of hashrefs, or as commonly known, an array of hashes aka an AoH. See perldsc for more on this.

    For object oriented programming in Perl, you going to need modules (see perlmod) and bless. If you're interested in OOP, you should probably look at perlboot and perltoot before anything else.