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

ok, there's gotta be a right way to do this, but I can't seem to see the forest for the trees..

Using the search method of a Class::DBI subclass, assigning to an array gets you the search results, assigning to a string gets you an iterator.. I want the results, not the iterator. I also want to try several slightly different searches and take the results from the first that gets any..

#!/usr/bin/perl use strict; my @results; # what I thought would work ok.. @results = blah('"or"') || blah('"or"') || blah('"or"'); # an attempt to fix.. @results = (blah('"(or)"')) || (blah('"(or)"')) || (blah('"(or)"')); # an ugly solution that works.. if (@results = blah('"if"')){ } elsif (@results = blah('"if"')) { } elsif (@results = blah('"if"')) { } # not as ugly, but still a hack for (1){ last if @results = blah('"for"'); last if @results = blah('"for"'); last if @results = blah('"for'); last; } sub blah{ my ($call) = @_; print "$call "; print wantarray ? "wants array" : "wants string"; print "\n"; return (1,2,3); } __END__ ]$ perl wantarr.pl "or" wants string "(or)" wants string "if" wants array "for" wants array

As you see I have 2 implementations that work ok, but I'm sure there's got to be a better way.. any suggestions?

cheers,

J

edited by ybiC: retitle from "how to wantarray..." for searchability

Replies are listed 'Best First'.
Re: Class::DBI and how to wantarray...
by liz (Monsignor) on Aug 03, 2003 at 12:25 UTC
    || 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

      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

(jeffa) Re: Class::DBI and how to wantarray...
by jeffa (Bishop) on Aug 03, 2003 at 13:48 UTC
    If you know you want the first (and possibly only) hit, then you can force array context with parentheses:
    my ($match) = DVD::movie->search_like(title => 'Alien%');
    $match is now a DVD::movie and not a Class::DBI::Iterator.

    Since you want to try several searches, i recommend trying out the techinique that Kake shows with Search::InvertedIndex in her perl.com article, How to Avoid Writing Code. Hope this helps. :)

    UPDATE: embarrassing typo/misinfo corrected by PodMaster.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)