wjw has asked for the wisdom of the Perl Monks concerning the following question:
There is of course the challenge that WhoIs responses are inconsistent, some having lots of information, others having much less. In fact the fields returned are not consistent. For example: Some records will return a key value pair for 'city', others don't.
Never-the-less, I have found a subset which seems to be there all the time, however the keys may be called out differently.
Again for example: email, e-mail, e_mail, abuse_email etc... .
Thus my delight when a google search for hashes with keys identified with regular expressions returned (from PM, mind you) Tie::Hash::Regex! And it works pretty nicely.(Thank you Mr. Cross!)
What I ended up doing to make it work however was to make a copy of the hash returned to my named reference($response) by 'whoisip_query' and tie to that copy. Again, this works, but I am wondering if there is a better way to do this. I tried tie-ing to '${%response}' to see if that would get me to the underlying hash, but that broke immediately. Following is what I tried:
#!/usr/bin/perl use strict; use warnings; use Tie::Hash::Regex; use Net::Whois::IP qw(whoisip_query); my %k_ip = ( "101.78.231.135" => 320, "103.23.141.141" => 300, "103.23.244.22" => 288 ); my $ref_r; #this works foreach my $ip ( keys %k_ip ) { print "$ip \n"; print "\t" . try1($ip) . "\n"; print "\t" . try2($ip) . "\n"; print "\t" . try3($ip) . "\n"; } print "all done\n"; sub try1 { my $ip = shift(@_); my %r; tie %r, 'Tie::Hash::Regex'; my $ref_r = whoisip_query($ip); %r = %{$ref_r}; my $email = $r{qr/mail/}; if( length($email) > 0 ) { return $email; } else { return "no email"; } } sub try2 { my $ip = shift(@_); my $ref_r = whoisip_query($ip); tie %{$ref_r}, 'Tie::Hash::Regex'; if( scalar( keys %{$ref_r} ) < 1 ) { return "tie did not work..emtied underling hash"; } my $email = %{$ref_r}{qr/mail/}; if( length($email) > 0 ) { return $email; } else { return "no email"; } } sub try3 { return "This blows up -> Can't locate object method TIESCALAR \t\tvia package Tie::Hash::Regex \t\tat /home/wjw/tmp/tie_hash_regex.pl line 56"; my $ip = shift(@_); my $ref_r = whoisip_query($ip); tie $ref_r, 'Tie::Hash::Regex'; if( scalar( keys %{$ref_r} ) < 1 ) { return "tie did not work.."; } my $email = %{$ref_r}{qr/mail/}; if( length($email) > 0 ) { return $email; } else { return "no email"; } }
Working with references is not my strong point(needless to say probably). My question(s) is/are:
Feel free to lecture me. My ignorance does not bother me... :-). It is Sunday morning and I am not all that analytic today.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tie::Hash::Regex to hash reference?
by Anonymous Monk on Apr 28, 2014 at 08:02 UTC |