in reply to Re^2: Populate Hash
in thread Populate Hash

That's not what I see when I run the code you posted:

Q:\>perl -w tmp.pl Global symbol "%myHash" requires explicit package name at tmp.pl line +4. Global symbol "$myHash" requires explicit package name at tmp.pl line +17. Global symbol "%myHash" requires explicit package name at tmp.pl line +22. Execution of tmp.pl aborted due to compilation errors. Q:\>type tmp.pl #!/usr/bin/perl use warnings; use strict; %myHash=(); sub populateHash { my $MY_FILE="file.properties"; open my $PFILE, '<', $MY_FILE or die "Cannot open '$MY_FILE' $!"; LINE: while (<$PFILE>) { # now read the line and remove all white spaces, tab and new line char +s or any t railing spaces $_ =~ s/\s+$//; next LINE if (/^\s*#/); # ignore commented lines next LINE if (/^$/); # ignore null lines my $n=substr($_,0,index($_,'=')); my $v=substr($_,index($_,'=')+1); $myHash->{$n}=$v; } close $PFILE; } &populateHash(); while ( my ($key, $value) = each(%myHash) ) { print "From myHash --> $key => $value\n"; }

Note that nowhere, %envPropertiesHash is mentioned. Please show some effort and actually make sure that the code you post exhibits the same problems as the code you run.

Replies are listed 'Best First'.
Re^4: Populate Hash
by uandme2k2 (Initiate) on Jul 27, 2009 at 14:18 UTC
    My apologies...I copied my initial before I had changed the code ....however for some reason I get the same error... Here is the code and output ...I appreciate your help.
    [/tmp]$ cat first.pl #!/usr/bin/perl use warnings; use strict; %myHash=(); sub populateHash { my $MY_FILE="file.properties"; open my $PFILE, '<', $MY_FILE or die "Cannot open '$MY_FILE' $ +!"; LINE: while (<$PFILE>) { # now read the line and remove all white space +s, tab and new line chars or any trailing spaces $_ =~ s/\s+$//; next LINE if (/^\s*#/); # ignore commented line +s next LINE if (/^$/); # ignore null lines my $n=substr($_,0,index($_,'=')); my $v=substr($_,index($_,'=')+1); $myHash->{$n}=$v; } close $PFILE; } &populateHash(); while ( my ($key, $value) = each(%myHash) ) { print "From myHash --> $key => $value\n"; } The OUTPUT is: ================ [/tmp]$ perl first.pl Global symbol "%myHash" requires explicit package name at first.pl lin +e 5. Global symbol "$myHash" requires explicit package name at first.pl lin +e 22. Global symbol "%myHash" requires explicit package name at first.pl lin +e 31. Execution of first.pl aborted due to compilation errors.
      Q:\>perl -Mdiagnostics tmp.pl Global symbol "%myHash" requires explicit package name at tmp.pl line +4. Global symbol "$myHash" requires explicit package name at tmp.pl line +17. Global symbol "%myHash" requires explicit package name at tmp.pl line +22. Execution of tmp.pl aborted due to compilation errors (#1) (F) You've said "use strict vars", which indicates that all variab +les must either be lexically scoped (using "my"), declared beforehand +using "our", or explicitly qualified to say which package the global var +iable is in (using "::"). ...

      I can't put it any better than diagnostics puts it.

      A reply falls below the community's threshold of quality. You may see it by logging in.

      The reason is that you refer to %myHash before having declared it. You need a my in front of %myHash like so:

      my %myHash;

      The =() doesn't do anything and can be omitted.