Problems writing stdout to a file, please help!

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 5
Reputation: peterv6 is an unknown quantity at this point 
Solved Threads: 0
peterv6's Avatar
peterv6 peterv6 is offline Offline
Newbie Poster

Problems writing stdout to a file, please help!

 
0
  #1
Oct 1st, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Problems writing stdout to a file, please help!

 
0
  #2
Oct 2nd, 2007
Have you defined one of the following variables in your script anywhere:
  1. IO::Handle->output_record_separator EXPR
  2. $OUTPUT_RECORD_SEPARATOR
  3. $ORS
  4. $\

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".
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 176
Reputation: trudge is an unknown quantity at this point 
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Problems writing stdout to a file, please help!

 
0
  #3
Oct 9th, 2007
Originally Posted by peterv6 View Post
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:

  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;

To open a file the 3-argument style with lexical filehandles is now generally recommended:
  1. my $outfile = "output.txt";
  2. 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
  3. print $FILE "whatever\n";
  4. 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 ..
  1. BEGIN
  2. {
  3. open (STDERR,">>$0-err.txt");
  4. print STDERR "\n",scalar localtime,"\n";
  5. }

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.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC