-1

I'm trying to reuse a library function we have in house, but due to new variants in my input things are not working right and throwing errors.

I realise the problem is that it is now trying to assign a funky sentence as the hash key such as the below list of keys and as you might expect, it's not liking it. Is there a way I can encode this before hashing it to prevent any sort of 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]))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user391986
  • 29,536
  • 39
  • 126
  • 205
  • 9
    Any string is a legal hash key in Perl, including all strings and substrings of what you posted. Also, almost any scalar value can be converted to a string and used as a hash key, including references to lists, arrays, and most class instances. So no, I wouldn't necessarily expect that your function that uses hashes isn't liking it. So what are the specific errors and gags you are trying to avoid? What is it about these inputs that are giving you trouble? – mob Oct 10 '11 at 22:55
  • 3
    @mob: Well said. Admittedly, using a stringified reference as a hash key isn't generally very useful, but one _can_ do it. In fact, the "almost" in your comment is unneeded: if asked to convert a scalar, _any_ scalar, to a string, perl _will_ convert it to a string, even if it might cough and splutter and emit warnings while doing it. (OK, I suppose one could make an overloaded scalar whose string conversion routine `die`s, but that's just plain silly.) – Ilmari Karonen Oct 10 '11 at 23:03
  • 1
    Perhaps you should show some code and errors. – TLP Oct 10 '11 at 23:38
  • mod -1: No code. Please post some code. It makes it easier to figure out what's going on. – David W. Oct 11 '11 at 00:58
  • Then for the not so feint of heart, there's [`Devel::Pointer::PP`](http://p3rl.org/Devel::Pointer::PP) to get your reference back. – Joel Berger Oct 16 '11 at 17:20

4 Answers4

8

Any string is allowed. Anything that's not a string will be stringified first.

ikegami
  • 367,544
  • 15
  • 269
  • 518
3

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";
Axeman
  • 29,660
  • 2
  • 47
  • 102
2

As others have stated, there is no limit to what is allowed as a hash key. If you use a reference, it will be turned into a string.

However, there are times when you don't need quotes and time when you do need quotes around your hash key. If you have spaces, or non-alphanumeric characters, you need quotes around your hash key. Interesting, you can use periods if you use only numeric characters. Otherwise, you can't use periods without surrounding your key with quotes:

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

And, to see what happens if you use a reference 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;

Produces:

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

So, the array reference was stringified, that is, coërced to a string.

Unfortunately, you posted no code, so we really can't say what your error is. Maybe you need to put quotation marks around your hash keys. Or, maybe there's another issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David W.
  • 105,218
  • 39
  • 216
  • 337
1

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

I raised an eyebrow at this when I discovered it by accident just now, but it's entirely consistent with the rules already stated in these answers: any string is allowed to be a hash key.

And in my case it's pretty useful.

Medlock Perlman
  • 147
  • 1
  • 10