There are conditions on my current macosx where an app will save a "plain text" version of a file using just CR for line termination, even though the OS is really a flavor of unix, and perl's default $/ on macosx is LF. (I can only wonder how much longer this CR silliness will go on.) I know that CRLF comes from a variety of web apps as well as from ms-win systems. Alas, $/ has to be a literal string -- you can't use a regex as the input record separator.
If you never encounter any really huge CR-format files, you might want something like this:
while (<>) {
my @lines;
if ( /\n$/ ) {
tr/\r\n//d;
@lines = ( $_ );
} else {
@lines = split /\r/;
}
for my $line ( @lines ) {
# do stuff with each line of text
}
}
Either that or else you just use slurp mode in all cases, and split into lines (using
/[\r\n]+/) if you really need to do that.
If you have to worry about getting really huge files in any of the three possible formats, you'll want to diagnose each file first -- at least, check the file size first, and if it's really big, read just enough (e.g. read FH, $_, 2048;), to figure out what the line termination is, set $/ accordingly, then rewind it (seek FH,0,0;) to read the whole thing as it was intended to be read.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.