SPUG: A dereferencing paradox?

Richard Anderson starfire at zipcon.net
Thu Sep 28 23:32:48 CDT 2000


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

#! /usr/local/bin/perl
@database = ( { name      => 'Wild Ginger', city      => 'Seattle',
                cuisine   => 'Asian Thai Chinese Japanese',
                rating    => 4, payment   => 'MC VISA AMEX', }
            );
%query = ( cuisine => 'Asian', );
@res = find_restaurant(\@database, \%query);
print "$res[0]{name}\n";

sub find_restaurant ($$) {
   use strict;
   my ($database, $query) = @_;
   return grep {
         $$query{city} ? lc($$query{city}) eq lc($_{city}) : 1 and
         $$query{cuisine} ? $$query{cuisine} =~ /$_{cuisine}/i : 1 and
         $$query{min_rating} ? $_{rating} >= $$query{min_rating} : 1 and
         $$query{max_rating} ? $_{rating} <= $$query{max_rating} : 1 and
         $$query{payment} ? $$query{payment} =~ /$_{payment}/i : 1
      } @$database;
}

In the subroutine, $database is a reference to an array of references to
anonymous hashes (whew!).  In the grep statement, the $_ variable should be
set to a reference to a hash.  So shouldn't I be using $$_{key} instead of
$_{key}?

But when I run the above code, it works:
./restaurant_search.pl
Wild Ginger

If I replace $_ with $$_ within the grep block, I get a newline only:
./restaurant_search2.pl

Any explanations?

Richard.Anderson at raycosoft.com
www.zipcon.net/~starfire/home (personal)
www.raycosoft.com (corporate)


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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