How does Perl variable scope work in while loop under strict mode? -
i'm new perl , i'm confused how variable scope works. i'm trying create array of hashes result of mysql query.
the following code works, intended, without use strict
%hash = (); while (my %hash = %{$qhand->fetchrow_hashref()} ) { push(@results, {%hash}); }
but when strict enabled produces following error:
can't use undefined value hash reference @ [filename] line xx (the line of while statement).
could tell me i'm doing wrong , corresponding rule in strict i'm flaunting?
you're violating refs
portion of strict. when try use non-reference value reference, perl wants create "symbolic reference", not want although silently continues program (probably not "working", continuing). enabling strictures, catch cases.
in example , jonathan's answer, looks doing lot of acrobatics undo hash references make them hash references again. there reason don't leave hash reference?
while( $href = $qhand->fetchrow_hashref ) { push @results, $href; }
and, if want results hash references, there's dbi method can skip while
loop:
my $results_array_ref = $qhand->fetchall_arrayref( {} );
Comments
Post a Comment