Lynn Glessner wrote:
> Hello, I've been lurking a little bit and wanted to ask a (hopefully
> simple) question.
>
> I use a perl script to convert some text files on my mac, but occasionally
> I want to put the text file on my windows machine and run my script there.
> Yay for the cross-platform abilities of perl :) Because windows and mac
> use a different code for their newlines my textfile has little square
> characters instead of carriage returns.
>
> I know I can use -0 on the command line to tell my script that $/ is
> something different, so that it correctly interprets \n, but haven't been
> able to figure out what that should be. I think I'm close, I expected 015
> to do it but it didn't.
>
> Currently if I need to do something like that I use a texteditor to resave
> the text file before running my script, but I would like to be able to
> leave out that step. So I think I could type "perl -0123 myscript.pl".
> Does it work like that? What is the code I would use for the mac text
> file?
Typically, if I don't know where a file has come from, I can run it through
a filter like this:
perl -pi.bak -e 's{\015\012|\015|\012}{\n}g' filenametofix
from the command line. this makes a backup of the original (as
filenametofix.bak) useful to know.
I'm not on my Mac at the moment, or I'd be happy to post a dropplet that
I've been using for some time to do the conversions via MacPerl. IIRC
something like this is part of the MacPerl distro, but also IIRC I altered
it slightly. (can't remember why) Feel free to fiddle around though.
You could always use something like this as an input filter, as well...
while (<>) {
s{\015\012|\015|\012}{/n}g;
# and if you don't actually need the newline after all...
chomp; # or simply change the above {\n} to {} instead.
# ...continue processing...
}