user_D_A__
user_D_A__

Reputation: 460

hash as reference throwing warnings of deprecation

I am having a perl subroutine in my perl script

sub constructSummaryString {
    my ( $SummaryHash, $SeverityHash, $Component ) = @_;
    foreach my $Key ( keys %$SummaryHash ) {
        print %$SeverityHash->{$Key};         # <---  Warning at line:163
    }
}

The input to the Subroutine are hash references.

When i try to run it I get a warning:

Using a hash as a reference is deprecated at xxxoo.pl line 163.

Upvotes: 1

Views: 93

Answers (3)

ikegami
ikegami

Reputation: 385897

The left hand side of -> must be a reference, so it must be a scalar. As such,

%$SeverityHash->{...}

should evaluate %$SeverityHash in scalar context.

>perl -E"my $SeverityHash = { a=>1, b=>2, c=>3 }; say scalar(%$SeverityHash)"
3/8

That means

%$SeverityHash->{...}

should be equivalent to

"3/8"->{...}   # Access the element of the hash named '3/8'.

Fortunately for you, there's a bug in Perl which causes the % to be ignored, so

%$SeverityHash->{...}

is actually equivalent to

$SeverityHash->{...}

but a deprecation warning tells you that you are relying on this bug.

Upvotes: 4

TLP
TLP

Reputation: 67918

The warning means what it says it means: You are using a hash as a reference, and that is deprecated. Look at the statement:

%$SeverityHash->{$Key}

Here, %$SeverityHash is your hash ref being dereferenced into a hash. This: ->{$Key} is you using that as a reference.

What you need to do is use the reference as a reference, and not try and dereference it.

$SeverityHash->{$Key}

Upvotes: 5

mpapec
mpapec

Reputation: 50647

Replace

print %$SeverityHash->{$Key}

with

print $SeverityHash->{$Key}

Upvotes: 2

Related Questions