in reply to Confused: Hashrefs in TieRegistry
I must seriously be missing something about references or objects or TiedHashes excetera
No, that part was fine. There are plenty of problems, however.
I have no idea what = $error is doing in
Dumper %RegHash = $error;
and you discard Dumper's output. Did you mean
print(Dumper(%RegHash));
@set = 2
should be
@set == 2
$error is printed, but nothing is ever assigned to it.
each returns false when there is no more data (which is not an error), so
each %RegHash or die
can't be right. Anyway, it's easier to work with foreach keys than while each because since can only use last safely with the former.
Why do you store a key in a variable called $val? $key would be better. $class would be ideal.
$watchcode isn't reset for each class, but it should be.
You print $! when it doesn't contain anything meaningful.
To use die when you can't find an association is quite brutal. warn (and maybe nothing at all) would be more appropriate.
Fix:
#!/usr/bin/perl use warnings; use strict; use Win32::TieRegistry (Delimiter => "/"); use Data::Dumper; foreach my $class (keys %{ $Registry->{"Classes/"} }) { next unless substr($class, 0, 1) eq '.'; my $watchcode; my $RegHash = $Registry->{"Classes/$class"}; foreach my $key (keys %$RegHash) { next if $key ne "/"; my $val = $RegHash->{$key}; print "$class = $val\n"; $watchcode = 1; last; } warn "failure to recognise association list\n" unless $watchcode; }
Notes:
I used next (twice) to avoid excessive nesting/indenting.
I added last in the inner loop. It's no use searching for a second key called /.
I made $RegHash a reference instead of an actual hash (%RegHash). It's much more efficient to refer to something than to make a copy of it.
Update: Fixed reference to $key that wasn't changed to $class (my mistake). $key swapped with $val (OP's mistake).
Update: The inner loop can be replaced with ifexists.
foreach my $ext (keys %{ $Registry->{"Classes/"} }) { next unless substr($ext, 0, 1) eq '.'; my $RegHash = $Registry->{"Classes/$ext"}; if (not exists $RegHash->{'/'}) { warn "Unable to recognise association list for $ext\n"; next; } my $class = $RegHash->{'/'}; print "$ext is a $class\n"; }
Tested.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Confused: Hashrefs in TieRegistry
by Maze (Sexton) on Aug 16, 2006 at 11:12 UTC | |
by ikegami (Patriarch) on Aug 16, 2006 at 12:10 UTC | |
by Maze (Sexton) on Aug 16, 2006 at 12:44 UTC | |
by ikegami (Patriarch) on Aug 16, 2006 at 12:09 UTC |