in reply to lexical scope in if/elsif tests

Here is my offering of code that avoids the issue and
#!/usr/bin/perl use warnings; use strict; my (%foos, %bars); my %targethash =( foo => \%foos, bar => \%bars, ); while( my $line = <DATA> ) { chomp $line; my ($name, $key, $val ) = $line =~ /^(\w+) ([^:]*):([^:]*)$/ ; if (! $name ){ warn qq( "$line" is the wrong format.\n ); }elsif (my $target = $targethash{$name} ){ $target->{$key} = $val; } else { warn "Line: $. has Invalid name '$name'\n"; } } __DATA__ foo key1:val1 bar key2:val2 foo key3:val3 qux key6:val6 bar key4:val4 3 bad:val 4bad:syntax

     Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Replies are listed 'Best First'.
Re^2: lexical scope in if/elsif tests
by AR (Friar) on Apr 05, 2010 at 19:41 UTC
    Much appreciated. I would vote you up if I could. I can't use your code because the example I posted was contrived. The real code I'm using has keys and values flipped based on which hash I'm putting them in. I'm doing it that way because it makes the config file more readable and the flow of the script more logical.
      It is easy enough to generate a "correctly" (in terms of ease-of-programming) directed hash from "flipped" values from a config file:
      Untested pseudo-code:
      my %correct; while (<config-file>){ ::Read data in the form $dest_hash_name, $text, [other info...] $correct{$text} = Generate_hashref($dest_hash_name); }

           Syntactic sugar causes cancer of the semicolon.        --Alan Perlis