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 | |
by edoc (Chaplain) on Aug 03, 2003 at 12:43 UTC | |
by liz (Monsignor) on Aug 03, 2003 at 12:54 UTC | |
by edoc (Chaplain) on Aug 03, 2003 at 13:07 UTC | |
by liz (Monsignor) on Aug 03, 2003 at 13:16 UTC | |
| |
|
(jeffa) Re: Class::DBI and how to wantarray...
by jeffa (Bishop) on Aug 03, 2003 at 13:48 UTC |