Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

filehandle in warning not what I expected...

by spstansbury (Scribe)
on Dec 27, 2010 at 20:26 UTC ( #879324=perlquestion: print w/ replies, xml ) Need Help??
spstansbury has asked for the wisdom of the Perl Monks concerning the following question:

Greetings...

I have a script that parses a csv file, looks up some data in another file, and generates a report. Pertinent code is here:

my $report_files_dir = "../Working/New_data"; my $filename_pattern = "Remedy.csv"; $interface_file = "nodes.csv"; open ( my $interface_data, "<", "$report_files_dir/$interface_file" ) +or die $!; opendir ( my $report_files, $report_files_dir) || die "can't opendir $ +report_files_dir: $!"; for ( sort readdir $report_files ) { next unless $_ =~ /$filename_pattern/; open ( my $remedy_file, "<", "$report_files_dir/$_" ) or die $!; while (<$remedy_file>) { chomp; next if ($. == 1); # Parse info from the current line # Use a returned value ($first_node) to look up data in anothe +r file: LOOKUP: while (<$interface_data>) { chomp; my @list = split/\|/; my $ref_id = &clean( $list[2] ); if ( $ref_id =~ /$first_node/ ) { # The CMTS name is in Column 0 $cmts_name = $list[0]; # The CMTS Interface is in Column 1 $cmts_interface = $list[1]; last LOOKUP; } } seek ( $interface_data, 0, 0 ); # Finish doing the report generation... } close ( $remedy_file ); } closedir ( $report_files ); close ( $benchmark_data ); close ( $interface_data ); close ( $progress_file );

But I get the following warnings:

Use of uninitialized value in subtraction (-) at ./load_Remedy.pl line + 503, <$interface_data> line 1894801. Use of uninitialized value in subtraction (-) at ./load_Remedy.pl line + 503, <$interface_data> line 5825736. Use of uninitialized value in subtraction (-) at ./load_Remedy.pl line + 503, <$interface_data> line 6546698.

The issue is not the uninitialized value ( I'm comparing two dates in line 503, and I may indeed be missing a date value in the original data, but the fact the the data file is $remedy_file, not $interface_data, and the line values are nonsensical, none of the files has more that 100K lines. Am I not using seek properly?

Comment on filehandle in warning not what I expected...
Select or Download Code
Re: filehandle in warning not what I expected...
by Corion (Pope) on Dec 27, 2010 at 20:47 UTC

    I think that you're throwing off Perl's line number counters by using seek.

    But if your files are not "that big", why do you search through the $interface_data every time on disk instead of reading it once into a hash?

Re: filehandle in warning not what I expected...
by Anonymous Monk on Dec 27, 2010 at 20:57 UTC
    Not related to your question, but when you use $filename_pattern = "Remedy.csv" and then /$filename_pattern/, that period character is used as the regex special character (like using [^\n]), which may match more than you intend (eg, it matches "Remedy_csv"). If you want to match strings in which that string occurs literally, quote it (like /\Q$filename_pattern\E/ or with quotemeta). However, I suspect you really want equality: $_ eq $filename_pattern;, or maybe case-insensitive equality (eg, for Windows): lc $_ eq lc $filename_pattern.
Re: filehandle in warning not what I expected...
by JavaFan (Canon) on Dec 27, 2010 at 21:31 UTC
    Perl only resets the line counter when opening the file. For each line in $remedy_file, you read many lines of $interface_data. It seems very inefficient to me, but that's a different issue. If, instead of seeking, you would just (re)open the file, you'll see more sensible numbers.

      Thanks for the input - you're right, it's inefficient, I will follow Corian's advice and read the file into a hash.

      But what really confused me is that the warning referenced the interface file and not the actual data file that the undef field was parsed from...

        Or it's simply showing the last filehandle that I read from...

Re: filehandle in warning not what I expected...
by ysth (Canon) on Dec 28, 2010 at 10:40 UTC
    Perl is just reporting the last file you read from. If you prefer to see the $remedy_file line number in the warning, you can localize perl's recollection of the last file read by putting:
    local $.;
    just before LOOKUP:. This is a special feature of the $. variable.
    --
    A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |
    Online Fortune Cookie Search

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-09-20 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (161 votes), past polls