0

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,
}; 
5
  • What is being passed to the script as arguments when you run it?
    – mbiokyle
    Aug 25, 2014 at 2:06
  • I have not gotten there - once I start passing arguments the errors/warning will go away?
    – capser
    Aug 25, 2014 at 2:10
  • Well normally the @_ array contains the arguments passed to a subroutine. Since this is not in a subroutine it is probably the reason for the error. Try changing the @_ to @ARGV and then passing the correct arguments.
    – mbiokyle
    Aug 25, 2014 at 2:14
  • yes - really was wondering what it was - and it is the subroutine array.
    – capser
    Aug 25, 2014 at 2:57
  • This program has never compiled in the form you show so you are not refactoring it. Why don't you tell us where it came from and what you are trying to achieve? It really isn't worth trying to fix 24 lines of badly-written code: you should write something new that follows a specification.
    – Borodin
    Aug 25, 2014 at 7:05

1 Answer 1

2

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.

2
  • excellent reverse engineering - this is part of a subroutine.
    – capser
    Aug 25, 2014 at 2:53
  • I commented out the my %extend_hash=%{@_[0]}; and the my @error_array=@{@_[1]}; and their are not warnings.
    – capser
    Aug 25, 2014 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.