Re: unfamiliar array reference



"Paul" == Paul Lalli <mritty@xxxxxxxxx> writes:

while ($array_ref = $sth1->fetchrow_arrayref) {
push @stash, [ @$array_ref ]; ##copy array contents

Paul> Is that seriously from the book? UGH. Just declare $array_ref in the
Paul> proper scope, and there's no need to take a reference to a dereference
Paul> of the reference.

Yes there is.

Paul> while (my $array_ref = $sth1->fetchrow_arrayref) {
Paul> push @stash, $array_ref;
Paul> }

Your code breaks on older versions of DBI, for which the book was written.
In older versions of DBI, the value of $array_ref would have been
the same for each row returned. There was this clause in "perldoc DBI":

Note that the same array reference is returned for each fetch, so don't
store the reference and then use it after a later fetch. Also, the elements
of the array are also reused for each row, so take care if you want to take
a reference to an element.

Yes, DBI has matured since the book, but I wouldn't jump to conclusions about
the validity of the code in the book. After all, Tim Bunce co-wrote the book,
and there's nobody on the planet who knows more about DBI.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@xxxxxxxxxxxxxx> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
.



Relevant Pages

  • Re: Is physics a science?
    ... it's true for all fluid mediums. ... Paul, I love it how you think a handbook of physics reference saves you from ... than aether which is primal) there exist measuring elements that ARE ...
    (sci.physics.relativity)
  • Re: unfamiliar array reference
    ... Paul> Is that seriously from the book? ... and there's no need to take a reference to a dereference ... Your code breaks on older versions of DBI, for which the book was written. ... Note that the same array reference is returned for each fetch, ...
    (perl.beginners)
  • Bible Quiz (Acts) 22/11/07 Cont.
    ... Reference: Acts 22-26 ... Where did Paul find an altar with the inscription, ... What Christian once lived at Rome, Corinth, and Ephesus? ...
    (uk.religion.christian)
  • Re: Heres one for Bob (hope it makes your head spin)
    ... to correct what I wrote - Paul is ... reference I can find to a reference to a reference to his writings dates to ... helped stone the first Christian Martyr. ... Give a man a fish, and he eats for a day. ...
    (comp.os.vms)
  • Re: Referencing System DLL
    ... where do I put the reference to it? ... Paul G. Tobey wrote: ... the BIB file to grab the compiled component and put it in the FILES ...
    (microsoft.public.windowsce.embedded)

Loading