Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Perl ignores CarriageReturn during fileparsing. Why?

by hoppfrosch (Scribe)
on Jun 13, 2013 at 12:00 UTC ( #1038727=perlquestion: print w/replies, xml ) Need Help??
hoppfrosch has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I've got a quite obscure problem concerning linewise parsing of files within my huge project.

Isolated problem:

  • I've got a simple file, containing two lines "A\nB\n"
  • I parse this file using the following:
    open( F, $file ) || die "Can't open $file - $!"; while (<F>) { print $_; next; }

Runnig this example standalone, everything works fine:

  • the file is processed line by line
  • $_ changes its value each iteration.
  • There are two iterations, as the file has two lines

I do have almost the same code within a huge project. Running this code on the same file does not show the same behaviour within my project:

  • File is opened correctly
  • ...but there is only one iteration
  • Introspecting into $_ shows, that the whole file is read as one line, showing an obscure separator betweeen "A" and "B" (something like "A|B").

What's going wrong? It used to work formerly - I made a couple of changes "far away" fom this code sequence within my project.

What may perl cause to ignore the CarriageReturn during parsing with while <F>? How can I "force" perl to use carriageReturn as line separator?


Replies are listed 'Best First'.
Re: Perl ignores CarriageReturn during fileparsing. Why?
by Athanasius (Chancellor) on Jun 13, 2013 at 12:08 UTC

    The input record separator is stored in the special Perl variable $/. It is newline (i.e., \n, not “carriage return”, which is \r) by default.

    Have your “far away” changes altered the value of $/?

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      You are my god for today - Wrong value within $/ (INPUT_RECORD_SEPARATOR) was the culprit; resetting it to '\n' solves my problem.
      I assumed there has to be a special variable for this - but couldn't find it ...
      Any change I made must have altered the value of $/ - have to find out which.
        Normally the input record separator should only be changed in the scope for which you need to change it
        # code "borrowed" from node 1952 :-) { local $/ = undef; open FILE, "myfile" or die "Couldn't open file: $!"; $string = <FILE>; close FILE; } # $/ reverts back to default here...
        If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038727]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-11-25 06:29 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (355 votes). Check out past polls.