cdarke has asked for the wisdom of the Perl Monks concerning the following question:
The call to this subroutine:my %drink; # other attribute hashes sub set { my ($key, $attr, $value) = @_; my $hashref; eval "\$hashref = \\\%$attr"; if ( !defined $hashref ) { carp 'Invalid attribute name'; } else { $hashref->{$key} = $value; } }
This does not work, i.e. it does not actually alter the 'real' hash (%drink). I have discover two fixes, but I cannot explain why either works 1. If I stringify the actual hash (%drink). After the assignment, if I add:set('larry', 'drink', 'Old Speckled Hen');
it works. It seems to 'commit' $hashref. Using Data::Dumper or Devel::Peek::Dump also fixes it. 2. If I make the hash an 'our' variable it works. I'm setting warnings and use strict. Tried on 5.8.6 on Fedora and 5.8.7 on Windows. What is going on?print "$drink{$key}\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: eval a reference to a my hash
by theorbtwo (Prior) on Apr 05, 2006 at 12:13 UTC | |
by cdarke (Prior) on Apr 05, 2006 at 12:37 UTC | |
by chromatic (Archbishop) on Apr 05, 2006 at 17:48 UTC | |
by cdarke (Prior) on Apr 07, 2006 at 10:13 UTC | |
by liverpole (Monsignor) on Apr 05, 2006 at 18:00 UTC | |
by cdarke (Prior) on Apr 07, 2006 at 10:10 UTC |