Contributed by --OutpostMir-- on Sep 06, 2001 at 07:17 UTC
Q&A  > CGI programming


I think I spent god-knows how long tonight working on setting up a Perl/CGI based image gallery for my site. Saddly, I finnaly get it coded properly (ha!) and I get this error when trying to use the script:

It looks like there was an error:
Your script produced this error: (Maybe you didn't strip carriage returns after a network transfer?)

My copy of Perl (ActivePerl) doesn't seem to have any tools to strip the errant carriage returns out of my script. What am I to do?

Answer: Removing carriage returns from source code
contributed by blakem

How about:

perl -pi.bak -e 's/\r\n/\n/g' perl -pi.bak2 -e 's/\r/\n/g'
This will translate both Mac EOLs and MS EOLs into Unix like ones.


Answer: Removing carriage returns from source code
contributed by tachyon

In case you are unfamiliar with using Perl directly on the command line the -pi.bak part makes a backup file with a .bak extension before the -e (for execute) executes the 's/this/that/g' on every line of For a more permanent solution just write a little script like this:

#!/usr/bin/perl -w -i.bak use strict; while (<>) { s/this/that/g; print; }

If you call this script say you use it like this:

C:\>type #!/usr/bin/perl -w -i.bak use strict; while (<>) { s/this/that/g; print; } C:\>type this.txt this this this this C:\>perl this.txt C:\>type this.txt that that that that C:\>type this.txt.bak this this this this C:\>

Just change this and that to whatever you want. For Mac that will be \r and \n respectively.

Answer: Removing carriage returns from source code
contributed by TStanley

And if you are using the vi editor on *nix, you can do the following:


To get the Control M character, press Control-V first, then Control-M. TStanley
Answer: Removing carriage returns from source code
contributed by Anonymous Monk

It might be those annoying ^M's.

while(<>) {
} <CODE>
% ./ < annoying_file.txt > ahhh.txt

Please (register and) log in if you wish to add an answer

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.