I'm storing a hash references from a threads to a shared @stories variable, and then can't access them.
my @stories : shared= ();
sub blah {
my %stories : shared=();
<some code>
if ($type=~/comment/) {
$stories{"$id"}="$text";
$stories{"$id"}{type}="$type";
lock @stories;
push @stories, \%stories;
}
}
# @stories is a list of hash references which are shared from the threads;
foreach my $story (@stories) { my %st=%{$story}; print keys %st; # <- printed "8462529653954" print Dumper %st; # <- OK my $st_id = keys %st; print $st_id; # <- printed "1" print $st{$st_id}; # <- printed "1/8" }
print keys %st works as expected but when i set in to a variable and print, it returns "1".
Could you please advice what I'm doing wrong. Thanks in advance.
In scalar context, keys %st
returns the number of elements in the hash %st
.
%st = ("8462529653954" => "foo");
$st_id = keys %st;
print keys %st; # "8462529653954"
print scalar(keys %st); # "1"
print $st_id; # "1"
To extract a single key out of %st
, make an assignment from keys %st
in list context.
my ($st_id) = keys %st; # like @x=keys %st; $st_id=$x[0]
print $st_id; # "8462529653954"