Legal Values ​​for the Perl Hash Key

I am trying to reuse the library function that we have in the house, but due to new options in my input, things do not work correctly and throw errors.

I understand that the problem is that now he is trying to designate the funky offer as a hash key, such as the list of keys below, and as you might expect, he doesn't like it. Is there a way I can code this before hashing it to prevent any gag from Perl?

Document: Multiple Attribute Equals (#root3 #form input[type=hidden], #root3 #form input[type=radio]) Document: Attribute selector using UTF8 (#root3 span[lang=À¸æ–‑]) Document: Attribute Ends With (#root3 a[href $= 'org/']) Document: Attribute Contains (#root3 a[href *= 'google']) Document: Select options via [selected] (#root3 #select1 option[selected]) Document: Select options via [selected] (#root3 #select2 option[selected]) Document: Select options via [selected] (#root3 #select3 option[selected]) Document: Grouped Form Elements (#root3 input[name='foo[bar]']) Document: :not() Existing attribute (#root3 #form select:not([multiple])) Document: :not() Equals attribute (#root3 #form select:not([name=select1])) 
-2
source share
4 answers

Any line is allowed. Everything that is not a string will be compressed first.

+7
source

As others have argued, there is no limit to what is permitted as a hash key. If you use a link, it will be converted to a string.

However, there are times when you do not need quotes and time when you need quotes around your hash key. If you have spaces or non-alphanumeric characters, you need quotes around your hash key. Interestingly, you can use periods if you use only numeric characters. Otherwise, you cannot use periods without quoting around your key:

 $hash{23.23.23} = "Legal Hash Key"; $hash{foo.bar} = "Invalid Hash Key"; $hash{"foo.bar"} = "Legal Hash Key because of quotes"; 

And to find out what happens if you use the link as a key:

 #! /usr/bin/env perl use strict; use warnings; use feature qw(say); use Data::Dumper; my %hash; my $ref = [qw(this is an array reference)]; $hash{$ref} = "foobar"; #Using Array Reference as Key say "\nDUMP: " . Dumper \%hash; 

It produces:

 DUMP: $VAR1 = { 'ARRAY(0x7f8c80803ed0)' => 'foobar' }; 

Thus, the array reference was shorthand, that is, it was connected to a string.

Unfortunately, you did not provide the code, so we really cannot say what your error is. You may need to put quotation marks around your hash keys. Or maybe there is another problem.

+2
source

Save this as a file and run it:

 use strict; use warnings; use Data::Dumper; my %hash; open( my $fh, '<', $0 ) or die 'Could not open myself!'; $hash{ do { local $/ = <$fh>; } } = 1; print Dumper( \%hash ), "\n"; 
+1
source

The empty string, "", is another legal value for the Perl hash key.

I raised an eyebrow when I discovered this by accident, but it is fully consistent with the rules already specified in these answers: any line is allowed to be a hash key.

And in my case it is very useful.

0
source

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


All Articles