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 (Cardinal) on May 18, 2017 at 10:31 UTC
Re: Use of uninitialized value in subroutine entry
by AnomalousMonk (Bishop) 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 (Canon) 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.