in reply to Re: advice for reading data from a file
in thread advice for reading data from a file

I agree with aragorn with getting rid of the die. I would not even bother with the warn unless I wanted to watch it. I think that it would better to send your errors to a log file with the file_name or any other stats so you can continue to process the correct files that would include logging files we can't open perhaps. Also, you are performing a regex on the return value which may be undef. I would think you should do the regex before you return the field if (log that too) in case that doesn't meet your criteria so you can be sure you have a valid return.
  • Comment on Re: Re: advice for reading data from a file

Replies are listed 'Best First'.
Re: Re: Re: advice for reading data from a file
by Anonymous Monk on Jan 18, 2004 at 19:26 UTC
    Also, you are performing a regex on the return value which may be undef.I would think you should do the regex before you return the field if (log that too)

    excuse me but i don't understand, here i'm not returning the value of the field but the returning value of the regexp which i think can be only 0 or 1 but i maybe wrong.

    do you mean that my sub can return 'undef' in some cases ?

      Yes exactly. If the regex doesn't matchm $fields2 will be undef. Also, I think you need to put parenthesis around your ( split /;/ ) so that the context is the same (list context):
      use strict; my @fields; my $RS = ' '; my $rv = &do_this; print $rv; sub do_this { while(<DATA>){ next unless $. == 2; @fields = ( split /$RS/, $_ ), last if $. == 2; @fields = ( split /$RS/, $_ ), last if $. == 2; } return $fields[2] =~ /\d+/; } __DATA__ This is line one. I found 12 on line two. And This was found on line three.

      Change 12 on line to some text and try removing the parens around the split... and you will see the diffs.
      JamesNC