Perl - unknown end-of-line character
I want to read an input file line by line, but this file has an unknown end character .
The editor vim
does not know this either, it represents this character as ^A
and immediately begins with characters from a new line. The same goes for perl
. It tried to load all lines once because it ignores this weird end-of-line character.
How can I set this character as end of line for perl? I don't want to use any special module for this (due to our strict system), I just want to define the character (possibly at hex
) the end of the line.
Another option is to convert the file to another one with a good end-of-line character (replace them). Can I do it in a simple way (something like sed
on the input file)? But everything has to be done in perl.
Maybe?
Now my reading part looks like this:
open (IN, $in_file);
$event=<IN>; # read one line
source to share
The character ^A
you mention is the "heading start" character. You can set a special Perl variable for this symbol $/
. Although, if you want your code to be readable and edited by the guy who comes after you (and uses a different editor), I would do something like this:
use English;
local $INPUT_RECORD_SEPARATOR = "\cA" # 'start of heading' character
while (<>)
{
chomp; # remove the unwanted 'start of heading' character
print $_ . "\n";
}
From Perldoc :
$ INPUT_RECORD_SEPARATOR
$ /The default delimiter for the input is newline. This affects Perl's understanding of what a "line" is.
Learn more about special escaping on PerlMonks.
Oh, and if you want, you can enter the "start heading" character in the VI, in insert mode , by pressing CTRL+ Vthen CTRL+ A.
edit: added local
per suggestion Drt
source to share