Robert Arnold wrote: > > Hello all, Hello,
> I have written a small script which uses Net::Whois::IP; and it > seems to work fine, but in attempt to make the script more > efficient, I've stumbled upon something that I don't understand. > > First of all, the purpose of the script is to take a list > of IPs and check their IP registrations. Actually the list of IPs > is in CIDR format, but I just schwick off the CIDR notation and > use whats left. So my list input looks like the following > (for the sake of prudence this example is using reserved space > rather than registered--but just to give an idea of the input): > > 10.0.1.0/24 > 10.0.2.0/24 > 10.0.3.0/24 > 192.168.0.0/24 > ... > > A couple blank lines are in the input, as well as a comment line or two > (#) which are just grepped out. I'm only interested in the "OrgName" > portion of the IP registration, which the module returns as an element > of a hash. So my script would output the above input like this: > > 10.0.1.0/24 Internet Assigned Numbers Authority > 10.0.2.0/24 Internet Assigned Numbers Authority > 10.0.3.0/24 Internet Assigned Numbers Authority > 192.168.0.0/24 Internet Assigned Numbers Authority > > Without the further ado, my current script: > > #!/usr/bin/perl -w > use Net::Whois::IP; > use strict; > > open(BLOCKS, "/home/me/cidr.txt") > or die ($!); > > my @blocks=<BLOCKS>; > > #grep out comments > @blocks=grep(/^[^#]/,@blocks); > > foreach my $range (@blocks) { > chomp($range); > > #shwick off cidr notation > my $ip=(split(/\//,$range))[0]; > > my %response = (); > > my ($response,$array_of_responses) = whoisip_query($ip); > > while ((my $key, my $value) = each %$response) { > if ($key eq "OrgName") > { > print "$range $value\n"; > #short circuits the while loop...i think? > last; > } > } #end of while > > } #end of foreach > > NOW, my question is this: If I can traverse the hash and look for a key > which is equal to "OrgName", why can't I simply print: $response{'OrgName'} ? > It just seems there should be a quicker, simpler way than having to > traverse the whole hash. There is. If a hash contains a certain key you can use exists() to see if it exists. > I'm guessing the answer has something to do with referencing/dereferencing, > which is a new subject for me. The perldoc says that $response is "a > reference to a hash containing all information provided by the whois > registrar". Yes, you have to dereference the hash reference. > ...so in the meantime, I will read up on references, but if someone > can usher me to the light of understanding otherwise, I would greatly > appreciate it :) Here is one way to do it: #!/usr/bin/perl -w use strict; use Net::Whois::IP; open BLOCKS, '/home/me/cidr.txt' or die "Cannot open /home/me/cidr.txt: $!"; while ( my $range = <BLOCKS> ) { my ($ip) = $range =~ m!^(\d+\.\d+\.\d+\.\d+)/\d! or next; my ( $response ) = whoisip_query( $ip ); print "$range $response->{OrgName}\n" if exists $response->{ OrgName }; } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]