CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [illafam] "/r" is only working for me on linux, not in dos: Edit Log


Mar 20, 2009, 8:25 AM

Views: 2954
Re: [illafam] "/r" is only working for me on linux, not in dos

Hi illafam,

Yeah, line termination is a b**ch, especially when dealing with files created on platforms different from the one you're running your perl program on.

A quick summary:

L/Unix systems: Lines end with a newline ('\n').
Windows systems: Lines end with a carriage return AND a newline ('\r\n').
Old Macs: Lines end with just a carriage return ('\r').

The traditional way to trim line termination in a perl script is with the chomp() command:

while (my $line = <IN>) {

The problem with this technique is that what characters chomp() exactly trims depends on the line termination in force in your perl program. Specifically, it depends on the value of $/.

If you look in the perl manual, you can see a bunch of blather about how to set $/ and how chomp is supposed to deal with it, but the bottom line is that you cannot guarantee on chomp to "do the right thing" for files not created on your system. Consequently, I never use it:

while (my $line = <IN>) {
$line =~ tr/\r\n//d;

Always works for me.

LIMITATION: <IN> will work for both l/unix and dos-style text using the default ('\n') $/ value. If you deal with Mac-style text (I never do) you'll have to do something else.



(This post was edited by 1arryb on Mar 20, 2009, 8:27 AM)

Edit Log:
Post edited by 1arryb (User) on Mar 20, 2009, 8:27 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives