GDBM_File WTF?

Ovid publiustemp-londonpm at yahoo.com
Thu Apr 5 16:11:17 BST 2007


--- David Cantrell <david at cantrell.org.uk> wrote:
 
>   use GDBM_File;
>   %db = ();
>   tie %db, 'GDBM_File', './db', &GDBM_WRCREAT|&GDBM_NOLOCK, 0600;
>   for (1..200) { $db{"foo$_"} = $_; }
>   while (my ($k, $v) = each %db) { delete $db{$k} if $v > 150; }
>   while (my ($k, $v) = each %db) { print "$k:$v\n" if $v > 150; }
> 
> prints entries for foo151 through to foo200.
> 
> What the fuck?

You're deleting from the hash while using the 'each' iterator.  You're
using this in a 'safe' way, so I'm wondering if this is a tie bug.  The
following is a workaround if you need it.
 
  use GDBM_File;
  %db = ();
  tie %db, 'GDBM_File', './db', &GDBM_WRCREAT | &GDBM_NOLOCK, 0600;
  for ( 1 .. 200 ) { $db{"foo$_"} = $_; }
  foreach my $k ( keys %db ) {
      my $v = $db{$k};
      delete $db{$k} if $v > 150;
  }
  while ( my ( $k, $v ) = each %db ) { print "$k:$v\n" if $v > 150; }

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/


More information about the london.pm mailing list