in reply to Understanding this line of Code

Let's take it a bit at a time, working out from the centre.

exec_select ($sql ) calls a function called "exec_select" passing it $sql.

We assume that this function returns an array reference, deference that reference and get the first element from the dereferenced array (that's @{ exec_select ($sql ) } [0]).

We then assume that the first element in that array contains a hash reference. We look for the "home1" key in the referenced array (that's @{ exec_select ($sql ) } [0] ->{ home1 }).

It's at this last stage that it goes wrong. We're assuming that we have a hash reference - but Perl finds undef instead. You (obviously) can't use undef as a hash reference so Perl throws an error.

You need to look at the value that is returned by exec_select. It is an array reference as expected, but the contents of that array (or, more specifically, the contents of the first element in that array) aren't what your code expects them to be.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Understanding this line of Code
by ptum (Priest) on Jul 19, 2006 at 03:29 UTC

    Notice how many times the word 'assume' crops up in davorg's excellent explanation? This is a place where ref() can be your friend, if only while debugging this problem. Try testing the various references to see if they are, in fact, array or hash references, like this:

    my $mystery_ref = exec_select($sql); my @temp_array = (); if (ref($mystery_ref) eq 'ARRAY') { @temp_array = @{$mystery_ref}; } else { print "Hey! I didn't get the array reference I expected, rather i +t is: ", ref($mystery_ref), "\n"; }

    If exec_select() returns an array reference, then you'll end up with that array in @temp_array, otherwise, you'll get a warning, telling you what kind of a reference it is.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde