in reply to Class::DBI and how to wantarray...

|| forces scalar context, so you're ||-ing the number of elements in the lists rather than the lists themselves.

Also, since database queries generally aren't cheap, I would build a foreach structure thus (in pseudo-code):

my @query = (set up list with queries); my @result; # set up result list foreach (@query) { last if @result = $_->result; }

Liz

Replies are listed 'Best First'.
Re: Re: Class::DBI and how to wantarray...
by edoc (Chaplain) on Aug 03, 2003 at 12:43 UTC

    yup, which is more or less what I ended up with, except it doesn't have to setup the queries (each is a hashref) unless it needs 'em:

    for (1){ last if @results = blah('"for"'); last if @results = blah('"for"'); @results = blah('"for'); last; }

    cheers,

    J

      Except in my case, it doesn't need to do all queries to get the hash refs. So you're only doing what is necessary, rather than to do it all and then select the first result.

      Liz

        ? That one doesn't do all three, it skips out of the "for" loop as soon as it gets a result..

        But thanks for the kick in the right direction with your first reply.. it got me scouring my copy of "Programming Perl". I think the closest I'm going to get to what I'm after is:

        #!/usr/bin/perl use strict; my @results; (@results = blah('"or1"')) || (@results = blah1('"or2"')) || (@results = blah('"or3"')); print join(':',@results)."\n"; sub blah{ my ($call) = @_; print "$call "; print wantarray ? "wants array" : "wants string"; print "\n"; return; } sub blah1{ my ($call) = @_; print "$call "; print wantarray ? "wants array" : "wants string"; print "\n"; return (1,2,3); } __END__ ]$ perl wantarr.pl "or1" wants array "or2" wants array 1:2:3

        (I extended the test a lil to make sure it was doing what I wanted..)

        cheers,

        J