Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
This line is coming after a db query,
$name = $sql->[0]{user} || '';

Its giving me an error that I am trying to prevent, but no luck, I even tried to assign an empty value as you can see on the above line of code, may be that's how I am not supposed to do this to avoid this:
Software error: Can't use string ("") as an ARRAY ref while "strict refs" in use at...

Any suggestions, thanks!

Replies are listed 'Best First'.
Re: Array ref error!
by kennethk (Abbot) on Mar 09, 2010 at 15:12 UTC
    To expound on what Corion said, the line $sql->[0]{user} is short hand for:
    1. Dereference the scalar value in $sql as an array reference.
    2. Get the first value in the array
    3. Dereference that as a hash reference.
    4. Get the hash entry for user

    Since the error says the interpreter expects an ARRAY ref, that means the error is in step 1. Examine the code around the line you've given us to determine what you've assigned to $sql. If this is unclear, see perlreftut for an broader introduction to references.

Re: Array ref error!
by Corion (Patriarch) on Mar 09, 2010 at 15:02 UTC

    The empty string is in $sql, so likely, $sql does not hold your result set from your database query, which is somewhat natural.

Re: Array ref error!
by DrHyde (Prior) on Mar 10, 2010 at 10:53 UTC
    What's the code that assigns to $sql?