Dru has asked for the wisdom of the Perl Monks concerning the following question:

Hello once again Monks,

I have the following program which produces the results that I want, but I'm getting the following messages along with it:
Use of uninitialized value in hash element at D:\scripts\whois.pl line 21, <FILE> line 1.
Here is the script. Constructive criticism welcome.
#Program used to get an ip address via a whois query using the clwhois tool use strict; use warnings; my $cmd = 'd:\clwhois.exe'; my $ip = '192.168.1.1'; my $file = 'd:\temp\output.txt'; my ($email, %fields); system("$cmd $ip >$file") and warn "Can not execute $cmd: $!\n"; open (FILE, $file) or die "Can not open $file: $!\n"; while (<FILE>){ /^(.*?): \s*(.*)$/; # Save text before colon in $1, and after # colon in $2 - Taken from the Camel Chp. 5.9 $fields{$1} = $2; # Creat %fields hash } # Save email address to $email depending on the whois db being used if ($fields{"TechEmail"} ne ""){ $email = $fields{"TechEmail"}; } elsif ($fields{"e-mail"} ne ""){ $email = $fields{"e-mail"}; } print "Email is $email\n";

Replies are listed 'Best First'.
Re: uninitialized value in hash element Error
by Paladin (Vicar) on Jul 03, 2003 at 19:54 UTC
    I'm taking a guess here since you don't give us an example of what output.txt should look like, but I am guessing it is in this part:
    while (<FILE>){ /^(.*?): \s*(.*)$/; # Save text before colon in $1, and after # colon in $2 - Taken from the Camel Chp. 5.9 $fields{$1} = $2; # Creat %fields hash }
    You are not checking to see if the RE matches before trying to use $1 and $2. You should probably have something like:
    while (<FILE>){ if (/^(.*?): \s*(.*)$/) { # Save text before colon in $1, and aft +er # colon in $2 - Taken from the Camel Ch +p. 5.9 $fields{$1} = $2; # Creat %fields hash } }
    Update: As AM pointed out below, you may even need more error checking depending on what your output.txt file looks like. Another Update: If I make the change I pointed out above to the code, and run it against the output.txt you posted below, I do not get an error message.
Re: uninitialized value in hash element Error
by Anonymous Monk on Jul 03, 2003 at 19:56 UTC

    One possibility is that your regexp isn't matching anything in the $1 group. Specifically, if your file contains a line like:
    : FOO!
    the match will succeed-- but with $1 undefined. The fix for this depends on what you want the code to do with lines formatted this way. Try making the * into a +, or making the whole thing greedy (although greed isn't always good; that'll break on lines like
    Foo: bar:baz
    where you get $1 = "Foo: bar" and $2 = "baz").

      Sorry about that guys, I meant to include output.txt, here it is:
      Command Line Whois Version 1.01 Copyright (C) 2003 Softnik Technologies Whois View - http://www.whoisview.com 192.168.1.1 OrgName: Internet Assigned Numbers Authority Address: 4676 Admiralty Way, Suite 330 City: Marina del Rey StateProv: CA PostalCode: 90292-6695 Country: US Comment: RegDate: Updated: 2002-09-12 AdminHandle: IANA-ARIN AdminName: Internet Corporation for Assigned Names and Number AdminPhone: +1-310-823-9358 AdminEmail: res-ip@iana.org TechHandle: IANA-ARIN TechName: Internet Corporation for Assigned Names and Number TechPhone: +1-310-823-9358 TechEmail: res-ip@iana.org # ARIN WHOIS database, last updated 2003-07-02 21:05 # Enter ? for additional hints on searching ARIN's WHOIS database. OrgID: IANA Address: 4676 Admiralty Way, Suite 330 City: Marina del Rey StateProv: CA PostalCode: 90292-6695 Country: US NetRange: 192.168.0.0 - 192.168.255.255 CIDR: 192.168.0.0/16 NetName: IANA-CBLK1 NetHandle: NET-192-168-0-0-1 Parent: NET-192-0-0-0-0 NetType: IANA Special Use NameServer: BLACKHOLE-1.IANA.ORG NameServer: BLACKHOLE-2.IANA.ORG Comment: This block is reserved for special purposes. Comment: Please see RFC 1918 for additional information. Comment: RegDate: 1994-03-15 Updated: 2002-09-16 OrgTechHandle: IANA-ARIN OrgTechName: Internet Corporation for Assigned Names and Number OrgTechPhone: +1-310-823-9358 OrgTechEmail: res-ip@iana.org # ARIN WHOIS database, last updated 2003-07-02 21:05 # Enter ? for additional hints on searching ARIN's WHOIS database.
      I tried changing * to +, but still the same thing.
Re: uninitialized value in hash element Error
by Cody Pendant (Prior) on Jul 03, 2003 at 23:44 UTC
    I think it's quite simple -- you're doing this:
    if ($fields{"TechEmail"} ne "")
    To see if there's anything defined for $fields{"TechEmail"}, which, as you're checking it, must sometimes be false.

    If you use "defined" it should go away:

    if ( defined ($fields{"TechEmail"}) )


    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
      Thanks all. Cody Pendant, I was still getting the error when using defined, but that looks cleaner, so I'm going to use it. Paladin, yup, you are correct, it works the way you have it. Thanks a million. I love this place. I need to go and donate some more money ;-)