capser
capser

Reputation: 2635

perl can't use undefined value as HASH reference

I am refactoring this script and I really do not understand the errors that I get.

#!/sbcd/GSD/scripts/perl/bin/perl
use DBI ;
use strict ;
use warnings;
use Data::Dumper;
my $dbUser = 'foo_01';
my $dbPass = 'foo_01';
my $dbSid  = 'foo.WORLD';
my $dbh =  DBI->connect("dbi:Oracle:$dbSid","$dbUser","$dbPass") or die( "Couldn't connect: $!" );
my %extend_hash=%{@_[0]};
my @error_array=@{@_[1]};
my @queries_array=();
my %spec_hash=();
my $query = "select e_risk_symbol from gsd_etds where level_name='EXCH_CS' and e_exch_dest='XISX' and e_symbol_comment in ('Bin_6','Bin_56')";

if(!$dbh) { 
    print "Error connecting to DataBase; $DBI::errstr\n"; 
}
    my $cur_msg = $dbh->prepare($query) or die "\n\nCould not prepare statement: ".$dbh->errstr;
    $cur_msg->execute();
    while (my @row=$cur_msg->fetchrow_array) {
         $spec_hash{'XISX'}{$row[0]}=1;
     }
print Dumper(%spec_hash);

these are the errors: I especially do not understand the can't use an undefined values as a HASH reference

walt $ ./pure_extend_database
Scalar value @_[0] better written as $_[0] at ./pure_extend_database line 11.
Scalar value @_[1] better written as $_[1] at ./pure_extend_database line 12.
Can't use an undefined value as a HASH reference at ./pure_extend_database line 11.

This is what I get when I with the warning and strict ;

$VAR1 = 'XISX';
$VAR2 = {
          'FCEL' => 1,
          'GPS' => 1,
          'MCO' => 1,
          'DPZ' => 1,
          'WM' => 1,
          'SPLS' => 1,
          'ILMN' => 1,
          'BWLD' => 1,
          'CTSH' => 1,
          'EWU' => 1,
          'MDVN' => 1,
          'PDCO' => 1,
}; 

Upvotes: 0

Views: 13407

Answers (1)

Slade
Slade

Reputation: 1364

The warning you're getting is caused by attempting to dereference an undefined value as a hash. The dereferencing as a hash is done with the %{ }, and the undefined value is the empty array slice you took with @_[0] (although what you actually want when you retrieve a single element of an array is the $ sigil, like the warnings tell you). The @_ array is only useful inside of subroutines, where it contains the arguments that the subroutine is called with.

For an intro to references in Perl, see perldoc perlreftut.

For explanations on particular warnings, see perldoc perldiag, or put use diagnostics; in your script to enable verbose warnings.

For the arguments given to the script on the command line, see @ARGV in perldoc perlvar or perldoc -v '@ARGV'. This probably isn't what you want, however; the code you are refactoring appears to have had a subroutine you removed, and there's no way to pass hard references to the script from the command line.

Upvotes: 2

Related Questions