The 'perldoc -f each' say me that is not safe delete or add a value while iterating, except if the item is the most recently returned by each().
When I run this code snnipet:
my ($key,$value);
my %fruits = qw/ banana 1 apple 2 grape 3 /;
while ( $key = each %fruits ) {
$fruits{$key} *= 7;
print "$key = $fruits{$key}\n";
}
print "\nRead only\n\n";
while ( ($key,$value) = each %fruits ) {
print "$key = $value\n";
}
everthing works fine!
But if I use a tied hash, humnn:
#-------------------------------------------------------------------------------
# select entries on database to print or to update.
sub select_urls {
my ($dbPath,$match,$newValue) = @_;
tie(my %tiedHash,'DB_File',$dbPath,O_RDWR|O_EXLOCK,0600,$DB_BTREE) || die("$program_name: $dbPath: $!\n");
while ( my($key,$value) = each %tiedHash ) {
if ( $key =~ $match ){
if ( defined $newValue ) {
$tiedHash{$key} = $newValue;
($key,$value) = each %tiedHash; # because 'each' come back 1 step when we update the entry
print "Value changed --> $key = $value\n";
} else {
print "$key = $value\n";
}
}
}
untie(%tiedHash) || die("$program_name: $dbPath: $!\n");
}
was necessary a second call to each().
I have to 'perl -v':
$ perl -v
This is perl 5, version 12, subversion 2 (v5.12.2 (*)) built for amd64-openbsd (with 8 registered patches, see perl -V for more detail)
Copyright 1987-2010, Larry Wall ...
I thinking if it's a bug?!!
Perhaps much more things are behind the scenes...
I asking if my solution is correct???