I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.

open STDOUT,"> ${output}" || die "$0 can't open $ifile";

This is how I'm printing:
print $_;

I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!

Have you defined one of the following variables in your script anywhere:

IO::Handle->output_record_separator EXPR 
$OUTPUT_RECORD_SEPARATOR 
$ORS 
$\

Because, that "^M" is the "\r" from a "\r\n" windows/dos style line ending rather than the standard "\n" of the unix/linux line ending (at least that is what I have always assumed it to be, since that is what I see when I copy a text file in binary mode in ftp from dos to unix). So, if running the code on a Linux machine produces those in the output file, I can only guess that you have defined one of the above variables to be "\r\n".

I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.

open STDOUT,"> ${output}" || die "$0 can't open $ifile";

This is how I'm printing:
print $_;

I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!

Um, STDOUT is generally your monitor and I don't see where you are opening a file for writing.

Where is $ifile defined?

First you should be using 'strict' and 'warnings' in your scripts, if you aren't already. This will save you hours of debugging time:

#! /usr/bin/perl
use strict;
use warnings;

To open a file the 3-argument style with lexical filehandles is now generally recommended:

my $outfile = "output.txt";
open (my $FILE, '>', $outfile) or die "Can't open $outfile: $!\n"; #ALWAYS check the result of an open, and use $! to tell you what happened
print $FILE "whatever\n";
close $FILE or die "Could not close $outfile: $!\n";

When I'm developing a script, I also put this block of code at the top of the script ..

BEGIN
{
	open (STDERR,">>$0-err.txt");
	print STDERR "\n",scalar localtime,"\n";
}

It writes the output of errors to a file in the same directory as the script, using the script name + '-err.txt' as a file name. Much easier than digging through server log files. When the script is working, just comment out the block.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.