Conversion of unix LF to DOS LF issues
master_son
created: 2006-02-01 16:39:40
Hello monks; The following is the code that I am using to upload a *.csv file via CGI and write its contents to another *.csv file However, when the destination file is written, I get blank rows in between each record.
	my $upload_dir = "some directory";
	my $evtFileName = 'testDoc';
	my $empFileName = $query->param('filepath');

	my $fh = upload('filepath');

	$fh =~ s/(?=\n)/\r/g;

	open OUTFILE, ">".$upload_dir."\\".$empFileName or die "Cannot open file ".$upload_dir."\\".$empFileName;

		while (<$fh>)
		{
			print OUTFILE;
			print;
		}
	close OUTFILE;


I am using CGI qw(:standard) and CGI::Simple CPAN modules for this

The code above contains the regex $fh =~ s/(?=\n)/\r/g; which I received earlier that is to convert UNIX or MAC CRs to DOS CRs. I was able to get it to do the work, but now it will not operate properly.

What am I doing wrong - or is there a good solution. Thank you in advance.

---------------------
Keep your concentration here and now where it belongs - Qui-Gon Jinn
Re: Conversion of unix LF to DOS LF issues
created: 2006-02-01 17:00:22
I seem to remember using binmode on the filehandles; that may do what you want without your having to do any line ending translation explicitly. That only is important on not-Unix systems as I recall.
Re^2: Conversion of unix LF to DOS LF issues
created: 2006-02-02 05:51:20

Indeed binmode with the appropriate layer (see PerlIO) or even directly specifying it in open's mode string may well be the best way to go.

I'd also recommend reading this thread in which a similar issue is discussed at length.

Re: Conversion of unix LF to DOS LF issues
created: 2006-02-01 18:26:26

You didn't specify on which system the script is running.

If you're running on Windows, you need to remove $fh =~ s/(?=\n)/\r/g; or add [doc://binmode|binmode(OUTFILE);]. print will automatically convert \n to \r\n when binmode isn't used.

If you're running on unix and the file is in unix format as you claim, it should work as is.

If you're running on unix and the file is in dos format, you need to replace $fh =~ s/(?=\n)/\r/g; with $fh =~ s/\r//g;.

Re^2: Conversion of unix LF to DOS LF issues
created: 2006-02-02 10:19:33
The first Bonus option worked for me. I tested it on the W2003 system and everything clicked. Thank you.
---------------------
Keep your concentration here and now where it belongs - Qui-Gon Jinn

perlmonks.org content © perlmonks.org and blazar, chas, ikegami, master_son

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03