Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a hash as a reference is deprecated

Tags:

perl

I have a problem with a Perl script but don't know why. I have something like this

..
$ref = pfget($Pf, "criteria");
%criteria= %$ref;
..
..
foreach $key (keys %criteria) {
  $expr = %criteria->{$key};
  ..
}

The line $expr = %criteria->{$key} gives me an error

Using a hash as a reference is deprecated

I've tried $expr = $criteria{$key} but the script is not working as it should be.

Have you got any idea where I am going wrong?

like image 854
peter Avatar asked Mar 14 '26 03:03

peter


1 Answers

%criteria in scalar context is suppose to return statistics about the hash.

$ perl -E'my %h = ( a=>1, b=>2, c=>3 ); say scalar %h'
3/8

So

%criteria->{$key}

means

"3/8"->{$key}

But a bug in Perl causes it to behave as

(\%criteria)->{$key}

The warning is telling you that your code is buggy because your code is relying on a bug in Perl. Use

$criteria{$key}

or avoid the wastefulness of creating %criteria and use

for my $key (keys %$ref) {
   ... $ref->{$key} ...
}
like image 161
ikegami Avatar answered Mar 16 '26 17:03

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!