On Aug 24, 9:03 am, [EMAIL PROTECTED] (Paul Lalli) wrote:
snip
>
> 'our', like 'my', is lexically scoped. Its effects are terminated
> when the innermost enclosing block ends. So if you're using the same
> package variable in two different blocks, you have to use 'our' in
> each of them:
>
Yes, that was a big revelation. I wasn't aware that declaring our in
a block made a difference. I thought that our meant it could be
declared once and used anywhere no matter where it was declared.
snip
> Your syntax for 'our' is confusing at best. Just declare it once, at
> the very top of your program.
Yes, I started declaring our at the beginning. Much clearer.
> From the output however, it looks like
> you have one of the older versions of DBI that Randal mentioned in his
> post. That is, you cannot do what I suggested. You have to make an
> explicit copy.
>
> my @stash; #there's no reason this has to be 'our' to begin with.
> while (my $array_ref = $sth1->fetchrow_arrayref) {
> push @stash, [ @{$array_ref} ];}
>
No, the version of DBI that I am using is very current so apparently
the DBI book is current on that issue also. Using your code above
(same as book) worked fine.
> Again. Stop using 'our' all over the place. Declare your variables
> once, at the beginning of your program, if it's being used throughout
> the program.
Yep, got it the first time.
> > # print "Row: $array_ref->[5]\n"; #Msg:Use of uninitialized value in
> > concatenation
> > # print $array_ref->[5]; #Msg: Use of uninitialized value in
> > concatenation
> > print "Row: @$array_ref\n"; #works, prints entire rows (from book)
>
> And what is the output of that? If $array_ref->[5] is undefined, then
> you do not have six elements in your result arrays, which means you're
> using a different query than the one you originally posted, and you'll
> have to adjust your code accordingly.
>
Yes, when I was experimenting I unknowingly deleted the first string
in my SQL, which made the reference to [5] nonsensical. Sorry.
> I wouldn't say "works" here. I would say nothing more than "doesn't
> generate compiler errors". This doesn't do what you think it does.
> This creates a reference to an *array* that contains the keys and
> values of your hash. You don't want to do that. You want to create a
> reference to a *hash*. To do that, we use { } instead of [ ]
>
> push @stash, { %{$hash_ref} };
>
Changed my code to this:
while (my $hash_ref = $sth1->fetchrow_hashref()) {
push @stash, { %$hash_ref }; #works, copies hash contents
}
> Right. All these messages are because you put array refs into @stash
> rather than hash refs.
Thanks again for all your help. I still need to digest all of the
posts regarding scope, but I understand it better than two days ago.
--pete link
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/