in reply to advice for reading data from a file

Seems perfectly reasonable to me. If this routine works for the files you have to process, it is correct. Maybe a warn instead of the dies in the routine can be used to tag the "corrupt" files so that the program doesn't bail out if only 1 or 2 files of the hundreds are faulty. But this may not be appropriate for your purpose.

Arjen

Replies are listed 'Best First'.
Re: Re: advice for reading data from a file
by JamesNC (Chaplain) on Jan 18, 2004 at 18:48 UTC
    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.
      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
Re: Re: advice for reading data from a file
by Anonymous Monk on Jan 18, 2004 at 19:02 UTC
    thanks for your answer,

    actually the code seems to works fine on the files.

    concerning the 'die' in the sub i need it because if one file is faulty the whole process need to be stopped.

    in fact i first check all the files type with an eval {} and the sub die in case of an error so i can catch it

    but i didn't tell about that in my post so thanks anyway :)