Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Module Error - defined(%hash) is deprecated

Background:

  • I am working to migrate a Linux server to a newer one from Ubuntu 10.04 to 12.04
  • This server is responsible for executing several a number of Perl modules via crontabs.
  • These Perl Modules rely heavily on 30-40 perl extensions.
  • I have installed all Perl extensions and the crontabs are able to process successfully except for several Syntax errors caused by the newer versions of these Perl extensions.
  • I need some help with modifying the syntax to get the Perl script to process as intended.

Errors:

defined(%hash) is deprecated at pm/Alerts/Alerts.pm line 943.
        (Maybe you should just omit the defined()?)
defined(%hash) is deprecated at pm/Alerts/Alerts.pm line 944.
        (Maybe you should just omit the defined()?)

Code:

###
    # Iterate the arrays deleting identical counts from each.
    # If we found a mismatch then die.
    # If either array is not empty when we are done then die
    $logger->info('Comparing ' . (scalar keys %cms_rows) . ' CMS symbols to ' . (scalar keys %stats_rows) . ' STATS symbols');

    foreach my $symbol ( keys %cms_rows ) {
    my %cms_row = delete $cms_rows{$symbol};
    my %stats_row = delete $stats_rows{$symbol};

##LINE 943##    die("Error: NULL CMS counts for symbol '$symbol'") unless defined %cms_row;
##LINE 944##    die("Error: NULL Stats counts for symbol '$symbol'") unless defined %stats_row;

    my $cms_json = encode_json(\%cms_row);
    my $stats_json = encode_json(\%stats_row);
    $logger->debug("Comparing counts for '$symbol': CMS($cms_json), Stats($stats_json)");

    die("Error: Up Counts Don't match for symbol '$symbol': CMS($cms_json), Stats($stats_json)") unless (!defined $cms_row{1} && !defined $stats_row{1}) || $cms_row{1} == $stats_row{1};
    die("Error: Down Counts Don't match for symbol '$symbol': CMS($cms_json), Stats($stats_json)") unless (!defined $cms_row{-1} && !defined $stats_row{-1}) || $cms_row{-1} == $stats_row{-1};
    }
    ###

Hopefully someone can help with this, any help is appreciated.

like image 384
virtual020 Avatar asked Dec 28 '15 19:12

virtual020


1 Answers

You must have upgraded from a seriously old version of Perl. The Perl 5.6.1 release notes say:

defined(%hash) is deprecated

(D) defined() is not usually useful on hashes because it checks for an undefined scalar value. If you want to see if the hash is empty, just use if (%hash) { # not empty } for example.

It was always a pretty stupid thing to do and Perl now warns you that you're doing something stupid. The warning is pretty clear about what you should do to fix this:

Maybe you should just omit the defined()?

So your lines would become:

die("Error: NULL CMS counts for symbol '$symbol'") unless %cms_row;
die("Error: NULL Stats counts for symbol '$symbol'") unless %stats_row;
like image 128
Dave Cross Avatar answered Oct 05 '22 14:10

Dave Cross