in reply to Hashing uninitialized values when no value returned from DBI sql select

In Perl 5.10, you can say,

$myhash{$val_1} = $val_2 // '';

Otherwise, I might say,

$myhash{$val_1} = defined $val_2 ? $val_2 : '';

Update: And you can always turn off warnings.

foreach $val_1 (keys %myhash) { no warnings 'uninitialized'; print "myhash{$val_1} = $myhash{$val_1}\n"; }

You can selectively and lexically turn off/on warnings as you like. Generally it's a good idea to limit the lack of warnings to the smallest scope possible. See perllexwarn for a list of the warnings categories you can toggle.

Replies are listed 'Best First'.
Re^2: Hashing uninitialized values when no value returned from DBI sql select
by punch_card_don (Curate) on Feb 27, 2008 at 20:51 UTC
    I thought I had 5.1, but I must not, because
    $myhash{$val_1} = $val_2 // '';
    produces
    Illegal division by zero
    on my system.
      I thought I had 5.1, but I must not, because
      5.10?

      Well, what does perl -v say?

        Yep - 5.8.8

      With Perl 5.8.8:

      $ perl -e '$v2=undef;$v1 = $v2 // "";' Search pattern not terminated at -e line 1. $ perl -e '$v2=undef;$v1 = $v2 / "";' Illegal division by zero at -e line 1.