Intercepting Perl through a hash causes a strange value

I use Perl to parse output from objdump. I have the following code:

#!/usr/bin/perl

%count = {};

while (<>) {
 if (/^\s+[[:xdigit:]]+:\s+[[:xdigit:]]+\s+([a-z]+).+$/) {
  ++$count{"$1"};
 }
}

while (($key, $val) = each %count) {
 print "$key $val\n";
}

In the resulting output, most parts are in order:

strhib 2
strcc 167
stmlsda 4
swivc 21
ldmlsia 4

But there is one strange line:

HASH(0x8ae2158) 

What's going on here? I expect it to $1be a string, but ++$count{"$1"}- fine.

Thank.

So the correct code should be:

#!/usr/bin/perl

use strict;

my %count;

while (<>) {
 if (/^\s+[[:xdigit:]]+:\s+[[:xdigit:]]+\s+([a-z]+).+$/) {
  ++$count{"$1"};
 }
}

while (my ($key, $val) = each %count) {
 print "$key $val\n";
}
+3
source share
1 answer

If you have use warnings;, you would see: "The link was found where even a dimensional list was expected." Instead

%count = {};

you should have said

my %count;

What you wrote is equivalent to this:

%count = ({} => undef);

hashref . hashref "HASH (0x8ae2158)" ( ). , parens (), {}. -.

, , :

use strict;
use warnings;

, , .: -)

warnings pragma -w, . . -w $^W perllexwarn.

+8

Source: https://habr.com/ru/post/1779817/


All Articles