SPUG: A dereferencing paradox?

Matt Tucker tuck at whistlingfish.net
Fri Sep 29 00:31:16 CDT 2000


-- Richard Anderson <starfire at zipcon.net> spake thusly:

> As the group pointed out during my presentation at the last SPUG
> meeting, the code in the following grep block looks wrong:
<snip>

Here's your problem:

>  $$query{cuisine} ? $$query{cuisine} =~ /$_{cuisine}/i : 1 and

This should read:

>  $$query{cuisine} ? $$_{cuisine} =~ /$$query{cuisine}/i : 1 and

(Or, as I'd write it, "$query->{cuisine} ...")

Since $_{cuisine} resolves to nothing (as it should; $_ is a hashref, 
and the hash %_ has nothing in it), you were trying to match the empty 
string against your search criteria, which would always succeed. In 
other words, you were trying to find "" in "Asian" instead of "Asian" 
in "Asian Thai Chinese Japanese".

Change all occurrences of $_ to $$_ and switch that regex around and 
it'll work properly.

Also, since you're doing a case-insensitive match, I assume that you're 
not expecting a regex in the query, in which case you might also want 
to bracket $query->{cuisine} with \Q...\E or even \b\Q...\E\b. Or maybe 
not.


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list