Deleting key/value pairs from perl result in Global symbol requires explicit package name error - perl

I am trying to delete certain key/value pairs from a hash, but I get the Global symbol requires explicit package name exception and I don't know how to debug this. I read up on some solutions, but none of them seem to work. So the hash is declared in this fashion:
my $hash = foo();
then I go through the hash using this line of code:
while (my ($key, $value) = each %$hash)
and in the block I select values I don't want and store the keys for these values in an array that was declared like this (before the loop of course):
my #keysArray = ();
I then access the array to retrieve the keys using this code so I can delete them from the hash:
for my $key (#keysArray){
delete $hash{$key};# this line of code is causing the problem
}
The last line that I wrote is the one causing the Global symbol "%hash" requires explicit package name exception.
Any fixes or am I doing something wrong here.
P.S. I changed the variable names and removed other internal code, but the format is the same.
Help please!
Thanks.

delete $hash{$key} deletes an entry from %hash. There is no %hash. Instead you want to write delete $hash->{$key}, which deletes an entry from %$hash.
I suggest perldoc perlreftut for answering all of your questions about references and how to use them.

You've declared $hash (a scalar reference to a hash) but not %hash (a hash). Try doing delete $hash->{$key} instead.

Your (repaired) code:
for my $key (#keysArray) {
delete $hash->{$key};
}
can be shortened to
for my $key (#keysArray) {
delete $$hash{$key};
}
or simply
delete #$hash{#keysArray};

Related

Perl - Global symbol requires explicit package name

I am calling a perl subroutine
&ProcessInput($line, \%txcountershash, \%txhash);
I have this definition:
sub ProcessInput() {
my $word;
my $counterkey;
my $line = $_[0];
my $countershash = $_[1];
my $outputhash = $_[2];
# remove all the blanks from the line
$line =~ s/\s+//g;
my ($port,$counter,$value,$datetime) = split('\|',$line,4);
my $counterdesc = $countershash{$counter};
the line that causes the problem is the last line. It says that global symbol %countershash requires explicit package name. I'm not sure why it's giving me the error. There aren't any problems otherwise, if I comment that line out the script runs. The hash is set up as an error code for a key and a description that is the value. I'm trying to get the value at a particular key in $countershash so I can add the error description to the output hash.
The problem is dereferencing. you should dereference the hash inside the subroutine
my $counterdesc = $countershash->{$counter};
-> this is called arrow operator which is used to deference the array and hashes.
The code $counterhash{$counter} means "look up the key $counter in the hash %counterhash". You don't have a hash called %counterhash, you have a hash reference in a scalar variable called $counterhash. %counterhash and $counterhash are two completely different variables.
In order to look up an element in a hash reference, you need to use different syntax.
my $counterdesc = $countershash->{$counter};
Please don't use the $$countershash{$counter} syntax (previously) used in another answer. That will only confuse whoever needs to maintain your code in six months time (which might well be you).

Perl: How to get keys on key "keys on reference is experimental"

I am looking for a solution to Perl's warning
"keys on reference is experimental at"
I get this from code like this:
foreach my $f (keys($normal{$nuc}{$e})) {#x, y, and z
I found something similar on StackOverflow here:
Perl throws "keys on reference is experimental"
but I don't see how I can apply it in my situation.
How can I get the keys to multiple keyed hashes without throwing this error?
keys %{$normal{$nuc}{$e}}
E.g. dereference it first.
If you had a reference to start off with, you don't need {} E.g.:
my $ref = $normal{$nuc}{$e};
print keys %$ref;
The problem is that $normal{$nuc}{$e} is a hash reference, and keys will officially only accept a hash. The solution is simple—you must dereference the reference—and you can get around this by writing
for my $f ( keys %{ $normal{$nuc}{$e} } ) { ... }
but it may be wiser to extract the hash reference into an intermediate variable. This will make your code much clearer, like so
my $hr = $normal{$nuc}{$e};
for my $f ( keys %$hr ) { ... }
I would encourage you to write more meaningful variable names. $f and $e may tell you a lot while you're writing it, but it is sure to cause others problems, and it may even come back to hit you a year or two down the line
Likewise, I am sure that there is a better identifier than $hr, but I don't know the meaning of the various levels of your data structure so I couldn't do any better. Please call it something that's relevant to the data that it points to
keys $hash_ref
is the reason for the warning keys on references is experimental
Solution:
keys %{ $hash_ref }
Reference:
https://www.perl.com/pub/2005/07/14/bestpractices.html/

Fixing keys that spring into existence when calling exist on a nested hash

Dear Stackoverflowers,
When calling exist on a hash for testing the existence of keys much further nested in a hash that does not exist, it will create keys leading to final test to see if the final key exist.
The example from perldoc exists is such:
undef $ref;
if (exists $ref->{"Some key"}) {}
print $ref; # prints HASH(0x80d3d5c)
I absolutely love the autovification feature of perl; however, I am now absolutely frightened of using exists for any future projects I may have.
Does anyone know if and/or how one may edit the library perl uses exists from or perhaps use a module to correct for this? It is really silly that if it does not exist, it makes one to see if future keys will exist.
Lastly, learning from the question below in Checking for existence of hash key creates key, one of the comments states OO-style for deeply nested hashes are suggested. Will there be any technical issues with deeply nested (>n=10) and high memory (>8GB) with very simple floating values stored in these nested hashes? Or just issues like this one?
Dereferencing undefined variables[1] is what causes autovivification[2]. Examples of dereferencing:
$ref->{key} and ${$ref}{key}
$ref->[0] and ${$ref}[0]
$$ref
#$ref
etc.
You can avoid it by replacing
exists( $ref->{"Some key"} )
with
$ref && exists( $ref->{"Some key"} )
or by adding
no autovivification;
Under certain circumstances.
"Autovivification" can also be used to refer to the creation of non-existent variables ($x = 1;), elements of hashes (my %h; $h{$key} = 1;) and elements of arrays (my #a; $a[3] = 1;). This post does not address these as it is not relevant here.
Try: perldoc -q multilevel
Or http://perldoc.perl.org/perlfaq4.html#How-can-I-check-if-a-key-exists-in-a-multilevel-hash%3F
You should add
no autovivification;
to the top of your module. This will prevent autovivification from creating the structure down to your test point.

Perl: reference to a hash to pass to another routine

In code that uploads excel spreadsheets it gives me the data in array ref:
for( #{$listref} ){...
I access it with $_->{'whateverthehashkeyis'} and have no problem.
What I need to do is pass the hash I am accessing in the current iteration of the loop to another subroutine.
This is where I am having problems. I have tried different things with no luck.
This DOES NOT work, but it should be an example of what I need to do
%args = #{$_};
$results = &format_trading_card_preview_item(\%args);
....
sub format_trading_card_preview_item
{
my %args = shift;
I think what I need to do is dereference the hash to send it over. Is that right?
Thanks in advance for any help
It looks like $listref is a reference to an array of hash references.
If you need to use the variable holding the hash references then it is better if you name that variable instead of using the default scalar $_
There is also no point in dereferencing the hash and copying it to %args, only to take a reference to that hash and pass it as a parameter to your subroutine
And it is wrong to call a subroutine with an ampersand & character, and has been so ever since Perl v5.5 landed over seventeen years ago
Your loop should look like this
for my $item ( #$listref ) {
format_trading_card_preview_item($item);
}
Within the subroutine, it depends a lot on what you want to do with the hash passed in, but you don't say anything about that, so it's probably best to leave it as a reference and write
sub format_trading_card_preview_item {
my ($item) = #_;
...
}
or you could use the statement modifier form of for, like this
format_trading_card_preview_item($_) for #$listref;
To answer your question, you don't need to dereference the hash reference in order to pass it to another subroutine. Creating a shallow copy and then taking a reference to that new hash is inefficient, but it would technically work just fine.
However, your problem is that you're confusing hashes and arrays by using the syntax to dereference an array reference on something that is actually a hash reference. In fact, you should have gotten an error message basically saying the same thing:
Not an ARRAY reference at foo.pl line ...
What you actually want to do is something like this:
for my $href (#$listref) { # variable names could be better
# do something
my $results = format_trading_card_preview_item($href);
# do something else
}
sub format_trading_card_preview_item {
my $args = shift;
print $args->{foo};
return 42;
}
Check out perlreftut and perlref for more information on Perl references and nested data structures.

Writing directly to a referenced variable in a subroutine from another module in perl

I am trying to pass reference of a hash into a subroutine defined in another module in order to increase performance. In the subroutine in this other module reference is dereferenced as:
sub subRoutine{
my $hash_ref = $_[0];
my %hash = %$hash_ref;
$hash{$a_key} = $a_value;
}
So this changes the value of that key in this module BUT it doesn't change the value in the perl program where this subroutine is called. What is the reason and how can I avoid this? Is it correct to use references instead of passing hashed/returning hashed in order to improve performance?
Thanks in advance!
Best wishes!
Passing references is fine if you want to manipulate the original variable, as you do here. The problem is that you are creating a copy of the referenced hash right away, and operating on the copy.
The line my %hash = %$hash_ref; is creating a new hash and copying all the key/value pairs from the original hash. When you change this copy it has no effect on the original.
Just get rid of that line, and replace the line $hash{$a_key} = $a_value; with $hash_ref->{$a_key} = $a_value;. You are now referring to an element of the original hash, and changing it.
Don't dereference into a local copy, just use the reference:
$hash_ref->{$a_key} = $a_value;
When you say %hash = %$hash_ref, you are dereferencing it to a local copy (subroutine scope). If you want to change the value of the hash you passed into the subroutine, use
$hash_ref->{$a_key} = $a_value.