Thanks for the update, now it's clear why Perl doesn't automagically know the input file information: You're reading the entire file into @lines, and by the time you write to output.csv, input.csv is already closed.

The quickest fix would be to implement the %SIG __WARN__ handler as described here. Here's a quick example (my input.txt contains just four lines, "foo", "bar", "quz" and "baz"):

open my $infh, '<', 'input.txt' or die $!; my @lines = <$infh>; close $infh; # open output file here my $i; local $SIG{__WARN__} = sub { my $msg = shift; $msg =~ s/\.?\s*$//; warn "$msg (input line $i)\n"; }; for($i=0;$i<@lines;$i++) { # do calculations and write to output file here # example warning: warn "found a z" if $lines[$i]=~/z/; } __END__ # Script Output: found a z at - line 14 (input line 2) found a z at - line 14 (input line 3)

Note that the "line numbers" reported here are 0-based, as it's actually an index into the array of lines. But the advantage is that here, the message is fully customizable, so you're free to print $i+1 or whatever else you like.

However, unless you've oversimplified your example, your code could be made more efficient if it were to keep the input file open while writing the output file. Also, as long as the input file stays open, Perl will normally add the input file line numbers to warning messages by itself. So for example:

open my $infh, '<', 'input.txt' or die $!; # open output file here while( my $line = <$infh> ) { # do calculations and write to output file here # example warning: warn "found a z" if $line=~/z/; } close $infh; __END__ # Script Output: found a z at - line 6, <$infh> line 3. found a z at - line 6, <$infh> line 4.

By the way, your code would be better if you used the three-argument open and error handling (or die), as in the above examples. Also, you really should look into Text::CSV for better handling of CSV files!


In reply to Re: Find data point generating Error in Perl code by Anonymous Monk
in thread Find data point generating Error in Perl code by kgherman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.