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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
"/r" is only working for me on linux, not in dos

 



illafam
New User

Mar 19, 2009, 6:33 PM

Post #1 of 5 (2234 views)
"/r" is only working for me on linux, not in dos Can't Post

Hi, I'm new to the forum, and took a quick search around, but haven't found exactly what I'm looking for...

I have a script I wrote that uses a lot of regular expressions to pull certain data from a txt file. A lot of the expressions use a "\r" to match the line end. This works fine when I run the script via Cygwin on my Windows XP machine. I then convert the script to a win32 binary with perl2exe and this no longer matches. I've read various articles on the web such as:

http://mag-sol.com/articles/crlf.html

This all makes sense to me, that different OS's use/interpret the text file with a different end line character. I'm guessing \r works fine for linux, but not dos. I tried to do a replace all on my script replacing all the \r with \l, but this did not do the trick either. At the end of the day I'd like the script to work on both OS's..

Can I do a either clause in my regular expression.. sort of like [\r\n].. except I would have to find the proper end line character for when running in dos?

Thanks... illafam.


1arryb
User

Mar 20, 2009, 8:25 AM

Post #2 of 5 (2229 views)
Re: [illafam] "/r" is only working for me on linux, not in dos [In reply to] Can't Post

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:

Code
... 
while (my $line = <IN>) {
chomp($line);
...
}


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:

Code
... 
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.

Cheers,

Larry


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


illafam
New User

Mar 22, 2009, 6:57 PM

Post #3 of 5 (2220 views)
Re: [1arryb] "/r" is only working for me on linux, not in dos [In reply to] Can't Post

Thanks Larry,

I'm not sure what happened.. but I gave it another go.. did a repalce all of ,y /r's with /n's and now works in Windows..

So the summary... the files Iam parsing aer all created on Windows machines...

If I run my script via Cygwin/linux shell on my windows xp machine.. \r works

If I run my script via active perl or compile it to an exe with perl2exe \n works for me..

I tried working with the Chomp but it didn't seem to help me.. I'm wondering if I can modify my script now with an OR condition so it will match \r or \n.. like \r|\n.. will give it a go


illafam
New User

Mar 22, 2009, 11:07 PM

Post #4 of 5 (2214 views)
Re: [illafam] "/r" is only working for me on linux, not in dos [In reply to] Can't Post

forgot to mention one thing.. to get this to work I also added the following while opening the file:

open (IN,"<:utf8",$file)

I guess that forces it to open in a certain manner


1arryb
User

Mar 23, 2009, 8:49 AM

Post #5 of 5 (2210 views)
Re: [illafam] "/r" is only working for me on linux, not in dos [In reply to] Can't Post

Hi illafam,

It's hard to help you when you don't post your code.

Cheers,

Larry

 
 


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

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