in reply to avoiding hash key typos

use warnings; use strict; my $X; $X->{aa} = 1; my $c = $X->{ab}; print "$c\n";
I get error: Use of uninitialized value in concatenation (.) at p1.pl line 6.

Replies are listed 'Best First'.
Re^2: avoiding hash key typos
by shemp (Deacon) on Apr 14, 2005 at 23:48 UTC
    Yep, you sure do.

    Im thinking more along the lines of when a hash key is supposed to be used in more than one place in a script and it is misspelled in one or more of them.

      Note that that error is not directly because the hash key is invalid. This would produce the same error:
      my $c = undef; print "$c\n";
      Anon's sample only emits a warning because it's using the bad value for something that doesn't accept undef's... but if you were doing something like the following, you would get silent failure/undesired results:
      my $ct = $dbh->selectrow_array("SELECT ... WHERE x = ?",{},$c);
Re^2: avoiding hash key typos
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 21:42 UTC
    "Use of uninitialized value in concatenation (.) at ..." is not an error. Unfortunately...

    I am used to seeing it quite often; God knows what the code around me does:-). Consider:
    #!/usr/local/bin/perl -w use warnings; use strict; my %report_hash = ( 'event_1_count' => 17, 'event_2_count' => 53, 'event_3_count' => 1245, ); my $salary = $report_hash{'salary'}; print "Conratulations! Your salary this month: $salary dollars.\n"; print "... and my reporting Camel goes on and on...\n"; # Now, this should do it: die "Oops... I did it again!.." unless exists($report_hash{"salary"});