Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Control M's with File::Copy

by Anonymous Monk
on Jul 21, 2003 at 14:41 UTC ( [id://276302]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how to remove the ^M after each line or can anyone tell me why this is happeneing in the first place using File::Copy on Windows?

This is my code:

#!/usr/bin/perl use strict; use warnings; use File::Copy; open(TEST, ">>Append_Data"); copy(".data_file", \*TEST); close TEST;
my output looks like this:
line1 ^M line2 ^M

Replies are listed 'Best First'.
Re: Control M's with File::Copy
by dragonchild (Archbishop) on Jul 21, 2003 at 14:45 UTC
    Try telling File::Copy that you're running on Windows. Also - are you opening the file in Unix or Windows? The ^M is usually seen when opening a Windows text file in Unix after ftp'ing it over in binary (instead of ASCII) mode.

    Also, it looks a little goofy that you're telling File::Copy to copy a filehandle you've opened in append-only mode ...

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Control M's with File::Copy
by snadra (Scribe) on Jul 21, 2003 at 14:50 UTC
    They are caused by Windows returns opened on a Unix system. For example after tranfering with FTP and using binary instaed of ascii mode.
    You can remove them later on by using s/\r//g

    snadra
      I've also seen them generated on a Unix system by certain programs. Another way to remove them would be using s/\015//g.


      "Ex libris un peut de tout"
Re: Control M's with File::Copy
by johndageek (Hermit) on Jul 21, 2003 at 17:34 UTC
    Assuming you are on the a unix box
    dos2unix utility will convert it quite nicely.

    John

Re: Control M's with File::Copy
by TomDLux (Vicar) on Jul 21, 2003 at 19:06 UTC

    I bet .data_file looks like that, too. There is no connection between copying the file and the presence of ^M. ^M is usually a problem when you copy a DOS file to Unix .... but you're on DOS, so it should be the normal state of affairs. What are you viewing the file with?

    As far as your code is concerned ...

    When you open() a file, it implies you are going to print() to the file.

    copy() does not require open()-ing a file.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      When you open() a file, it implies you are going to print() to the file.

      copy() does not require open()-ing a file.

      Actually, File::Copy will accept a filename or a filehandle, opened for whatever reason. It's poor coding style, but it (apparently) will work.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Control M's with File::Copy
by kryberg (Pilgrim) on Jul 23, 2003 at 14:47 UTC
    This is my favorite node on Perl Monks because I have found it so useful, since I work in a dual Unix/Windows environment and have the ^M problem frequently. It's an easy way to remove the ^M.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://276302]
Approved by allolex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found