Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

"Modification of non-creatable array value attempted, subscript -1" Error

by techie411 (Acolyte)
on Oct 26, 2011 at 06:10 UTC ( #933791=perlquestion: print w/ replies, xml ) Need Help??
techie411 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am a Perl beginner and I have a whois script that takes IPs from a file and is supposed to grab the ARIN whois information, however I am getting an error:

"Modification of non-creatable array value attempted, subscript -1 at /usr/lib/perl5/site_perl/5.8.8/Net/Whois/ARIN.pm line 391."

Can anyone let me know what I'm doing wrong? Thank you.

#!/usr/bin/perl use Net::Whois::ARIN; my $file="file_containing_IPs.txt"; open(LIST,$file) or die "Unable to open file $file:$!\n"; my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); while(<LIST>) { foreach ($_) { chomp; my @output = $w->network($_); foreach my $net(@output) { printf( "CIDR: %s\tNetName: %s\tNetHandle: %s\n", $net->CIDR, $net->NetName, $net->NetHandle, ); } } } close(LIST);

Comment on "Modification of non-creatable array value attempted, subscript -1" Error
Download Code
Re: "Modification of non-creatable array value attempted, subscript -1" Error
by MVS (Monk) on Oct 26, 2011 at 06:37 UTC

    If possible, try updating your copy of Net::Whois::ARIN to the latest version (0.12). By looking at the change log and the module code, it seems that you're running into a bug that was recently fixed:

    Revision history for Perl extension Net::Whois::ARIN. 0.12 Sat Jun 11 07:52:36 2011 - fixed point-of-contact parsing bug

    Note the changes made to the contact subroutine of the module source code (lines 393 and 394) to eliminate the possibility of storing data in $records[$n] when $n is -1.

Re: "Modification of non-creatable array value attempted, subscript -1" Error
by BrowserUk (Pope) on Oct 26, 2011 at 06:39 UTC

    I don't have the module installed, but what happens if you remove the (pointless) outer for loop?:

    while(<LIST>) { chomp; my @output = $w->network($_); foreach my $net(@output) { printf( "CIDR: %s\tNetName: %s\tNetHandle: %s\n", $net->CIDR, $net->NetName, $net->NetHandle, ); } }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: "Modification of non-creatable array value attempted, subscript -1" Error
by GrandFather (Cardinal) on Oct 26, 2011 at 06:59 UTC
    Can anyone let me know what I'm doing wrong?

    Well, a bunch of stuff really. Or at least, if not wrong, then sub-optimum. But not so much that would generate the error you describe. My best guess at the cause of your error is that one of your calls to the module is failing (no entries in a list perhaps) and that code that is called subsequently is making bad assumptions. The error itself is caused by trying to modify the last element (the -1 bit) of an empty array - the element doesn't exist and can't be modified. You are using a rather old version of Perl and possibly an old version of the module so the first thing that would be worth trying is updating to the latest version of the module.

    As for the other stuff I alluded to:

    1. Always use strictures (use strict; use warnings; - see The strictures, according to Seuss)
    2. Use the three parameter version of open and use lexical file handles: open my $inFile, '<', $fileName or die "Can't open $fileName : $!"
    3. foreach ($_) only iterates once so isn't doing anything interesting in your code
    4. Generally use explicit variables rather than the default variable in general code to make the code easier to read and to avoid nasty bugs due to $_'s contents changing at unexpected times

    I'd rework the code to look something like the following (untested):

    #!/usr/bin/perl use strict; use warnings; use Net::Whois::ARIN; my $file = "file_containing_IPs.txt"; open my $inFile, '<', $file or die "Unable to open $file: $!\n"; my $w = Net::Whois::ARIN->new ( host => 'whois.arin.net', port => 43, timeout => 30, ) or die "Can't access server: $!\n"; while (defined (my $line = <$inFile>)) { chomp $line; next if !length $line; for my $net ($w->network ($line)) { printf "CIDR: %s\tNetName: %s\tNetHandle: %s\n", $net->CIDR, $net->NetName, $net->NetHandle; } } close $inFile;
    True laziness is hard work
Re: "Modification of non-creatable array value attempted, subscript -1" Error
by Anonymous Monk on Oct 26, 2011 at 07:04 UTC
    My brother at one time, long ago in the mid 90's, tried to create a program in Visual Basic without any programming experience... --He threatened to name his next dog, "Subscript Out Of Range." An array always has a size, even if it's empty. When you access array elements by their index number, you are, in less common terms, actually using a "subscript" number. In perl, array index/subscript numbers are positive integers starting at 0 for the first element. If an array is accessed in scalar context, it will return the number of elements in the array. If an array is empty in perl (no elements) and accessed in scalar context, it will return -1, an invalid index/subscript for all arrays. As a perl beginner, you're missing some of the basics, namely always use 'strict' and 'warnings' and while developing, also using 'diagnostics' can be real helpful.
    #!/usr/bin/perl use strict; use warnings; use diagnostics; # this makes error messages nicer.
    As for the problem with your code, I'd bet you have a blank line in your input file. You're not checking $_ (default input), so you'd be feeding an empty string into Net::Whois::ARIN.
    #!/usr/bin/perl use strict; use warnings; use diagnostics; # this makes error messages nicer. use Net::Whois::ARIN; my $file="file_containing_IPs.txt"; open(LIST,$file) or die "Unable to open file $file:$!\n"; my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); while(<LIST>) { foreach ($_) { chomp; if(defined $_ && $_) { my @output = $w->network($_); foreach my $net (@output) { printf( "CIDR: %s\tNetName: %s\tNetHandle: %s\n", $net->CIDR, $net->NetName, $net->NetHandle, ); } } } } close(LIST);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://933791]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-09-30 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (364 votes), past polls