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

Re: [MacPerl] Writing line-ending agnostic scripts?



Michael Schuerig <uzs90z@ibm.rhrz.uni-bonn.de> writes:
}
}Similar problems occur in lots of other cases where files from different
}platforms are involved, especially when they're acquired in archives and no
}implicit conversion is done.
}
}I'd like best if MacPerl had an option to treat \015, \012 and \015\012 as
}line endings.

But it already does!  Tell me why something like this won't work for you
(fix the inevitable mistakes, because I'm going to write this off the top
of my head). $file contains the path to a known-to-be-text file:

open(IN,"<$file") or die "Unable to open $file";
read(IN,$block,1024);
$m = ($block =~ tr/\015/\015/);
$j = ($block =~ tr/\012/\012/);

if($m && !$j) { # pure mac text
	$/ = "\015";
} elsif ($j && !$m) { #pure unix text
	$/ = "\012";
} elsif ($m == $j || $m-1 == $j) { #droppings of the Spawn of Hell,
                                   #watch for splitting \015\122
	$/ = "\015\012";
} else { #Can't figure it out?  Stay native
	$/ = "\n";
}
seek(IN,0,0); #Now that that's over with, get ready for line-by-line reading

You can adjust the conditionals as required.

}Thinking of it this might be very useful when Rhapsody
}arrives as we probably have to get used to different line-endings in files
}created on the same machine.

You're asking for a large change in the C source of Perl to fix a problem
that a few lines of Perl will solve.  Developers will know for sure in a
few weeks, but I'd guess that Rhapsody will either have automatic
translation built in, so that a Yellow Box text editor can natively read a
Blue Box text file and vice versa, or will use \015 as the end-of-line in
both boxes.

}
}Michael


---
Paul J. Schinder
NASA Goddard Space Flight Center
Code 693, Greenbelt, MD 20771
schinder@pjstoaster.pg.md.us



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch