tobias_hofer has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I am working in tooling project. And are using perl for code (C-Code) generation. My part in this is a bit different i use perl for generating a test-configuration. At this moment i want to read-in a config file to analyze it line by line. (Yes, i am using strict, warnings and diagnositcs!)

Just like this:
open( my $FILE, "< $TestBench_File" )|| die "Error[...]"; my @content = <$FILE>;


I expect that @content is an array with an item per line in the file. This works often. But every now and then @content has only one Element and the element is the complete content of the file (with all linebreaks and stuff). My wild guess: somewhere in the code-gen part the io is set to something special. But i have no idear where and how.. I asked my colleagues but none of them are aware of this.

Does anyone know how to solve this? Yes, i could do a split on the one array-elemet.. but do not feel that this is a good approach.. only a fallback solution...

Help is highly welcome!!

Best regards Tobias

Replies are listed 'Best First'.
Re: Reading in of a file
by choroba (Cardinal) on Nov 07, 2014 at 10:22 UTC
    It seems like somewhere in the code, the global variable $/ gets modified. You should always localize such changes, but if that doesn't help, try setting
    local $/ = "\n";

    before reading the whole file into the array.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      This is it! Great!!!

      Yes, i will localize it just like your example.

      Thank you very much, you made my day and weekend! ;-)

      Best regards
      Tobias
Re: Reading in of a file
by Your Mother (Archbishop) on Nov 07, 2014 at 16:51 UTC

    One more note though your real problem is solved. You should consider adopting the three argument open, like so–

    open( my $FILE, "<", $TestBench_File ) || die "Error[...]: $!";

    More on the topic via Google.