Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Use of uninitialized value in subroutine entry

by Anonymous Monk
on May 18, 2017 at 10:24 UTC ( #1190519=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I do not have any knowledge of perl at all. Downloaded a script to parse information from a website to 2 separate csv files and i keep the same error. The error points to the lines that start with "$csv->print" and says "Use of uninitialized value in subroutine entry". Please help.

#Fixing IPs if (defined $record[7] and $record[7] ne '' and $record[7] ne '1 +' ) { my @ips = split /\|/,$record[7] ; foreach my $ip (@ips) { $record[7]=$ip; $csv->print ($ips, \@record); } } #Fixing Domains if(defined $record[2] and $record[2] ne '') { $csv->print ($hosts,\@record); }

Replies are listed 'Best First'.
Re^5: "Use of uninitialized value in subroutine entry" warning with HTML::TableExtract
by marto (Bishop) on May 18, 2017 at 10:31 UTC
Re: Use of uninitialized value in subroutine entry
by AnomalousMonk (Canon) on May 18, 2017 at 18:15 UTC

    Please also see Short, Self-Contained, Correct Example. Note that the exercise of creating a short code and data example for us may clarify to you what the problem is without need for further consultation.

    BTW: From the very limited code presented in the OP, one can guess that the problematic values in the subroutine entries of the
        $csv->print ($ips, \@record);
        $csv->print ($hosts,\@record);
    statements are $ips and $hosts. If @record were not defined, nothing at all would work (I doubt the code would even compile), so pay particular attention to these former variables.

    Give a man a fish:  <%-{-{-{-<

Re: Use of uninitialized value in subroutine entry
by Marshall (Abbot) on May 20, 2017 at 07:15 UTC
    In your code, you appear to have an "s" in the wrong place:
    foreach my $ip (@ips) { $record[7]=$ip; $csv->print ($ips, \@record); #Error probably here (ips -> ip) }
    probably what you need is: $csv->print ($ip, \@record);.

    If you were using use strict; use warnings; This should throw a compile error because the scalar $ips is not defined.

    One thing to consider is avoiding variables that differ by just a single letter (ips vs ip). It is very easy to make a typing mistake and get confusing results. Also be aware that in Perl, a symbol like "ips" can be both an array variable @ips and a different scalar variable like $ips at the same time. Not everything that is possible is wise. Just for consideration, assuming you keep @ips as is:

    foreach my $cur_ip (@ips) #or perhaps $this_ip or other name? { $record[7]=$cur_ip; $csv->print ($cur_ip, \@record); }
    Of course I have no context, but probably I would wind up changing @ips to something else: @local_ips, @network_ips, some name describing what kind of ips these are and keeping the simple $ip for the loop iterator name.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1190519]
Approved by beech
Discipulus must prepare some ammunitions: XML::Compile::SOAP but what the heck!

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-06-27 09:04 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (601 votes). Check out past polls.