The reason get_ids({'id_one','id_two','id_three'}) doesn't work is because it is taking each pair of values as a key/value pair. So that line is constructing a hash { id_one=>'id_two', id_three=>undef }.
If you were running with warnings enabled, you would be receiving a message Odd number of elements in hash assignment at... which would have given you a clue here
Without asking why you want to do this, though from what you've told us it seems bizaar:), you could do
get_ids( {id_one=>undef, id_two=>undef, id_three=>undef } );
However, if the idea is that you are going to return the hash to the caller with the values for the keys supplied filled in, it might be better to pass a ref to an anon. array of the values and then construct the hash internally like this.
sub get_ids { my %hash; @hash{@{shift()}} = undef; print "key:$_\n" for keys %hash; return \%hash; } # Call it this way my $hashref = get_ids( [qw/id_one id_two id_three/] );
If you run that, you'll get output
key:id_one key:id_three key:id_two
In reply to Re: Pass anonymous hash - keys only?
by BrowserUk
in thread Pass anonymous hash - keys only?
by blahblah
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |