[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-AnyPerl] change input line termination



Folks,

I work with a variety of HTML source files (DOS, UNIX, Mac) and want 
to prepend a DOCTYPE line. A previous script version worked well with 
Mac files but put DOS/UNIX output all on one line after processing. 
The goal is to have the script function as a droplet returning Mac 
files for further editing.

I consulted a couple of sources with different regex solutions, none 
specifically for my task (usually Socket-related work.) I tried 
several alternative methods today with not much success. Here's the 
script section:

#---
# input and temp file names defined previously...

# read input file lines into array elements
open (INPUT, "<$inputfile") or die "Can't open $inputfile for reading - $!";
@lines = <INPUT>;
close (INPUT);

# open output file for writing
open (TEMP, "> $tempfile") || die ("Could not open $tempfile for writing.\n");

# localize the line endings with a substitution and
# write file to output

while ( defined($line = shift (@lines)) ) {
	if ($^O =~ /MacOS/) {
		print TEMP $line;
	} else {
		local $/ = "\012";
		$line =~ s/\015?\012/\015/g; # (Unix or DOS to Mac)
		print TEMP $line;
	}
} # end while
#---

Previously, I had:

#---
open (INPUT, "<$inputfile") or die "Can't open $inputfile for reading - $!";
# code for checking if doctype line, etc. and adding it if needed here...
open (TEMP, "> $tempfile") || die ("Could not open $tempfile for writing.\n");
print $addedline, "\n";
print TEMP <INPUT>;
#---

Simpler, but had problems with DOS and UNIX files, so I modified the 
script shown above.

My Mac editor, BBEdit, is smart enough to display any source file 
correctly when opened but after it goes through this script, the 
output file shows text on each line (Mac input), text all on one line 
(UNIX input), and text on each line with an added \n char 'box' 
character (DOS input.)

Surely, there is a portable perl idiom for this task. I flip between 
DOS, VMS, UNIX and Mac OSes to work with files. Some files are 
dropped in a Windows directory for my VirtualPC pickup. Some arrive 
in email zipped or attached, etc.

Any suggestions?

Paul
----


==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org