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

Re: [MacPerl] UNIX to Mac linebreaks



Chris Nandor wrote:
> 
> Does anybody know of a droplet or something to change UNIX linebreaks to
> Mac linebreaks?  I have BBEdit, but I wanted something to do it in batches,
> drag-n-drop style.
> 

Try the following. It works for me, subject to these caveats. I have
found that MacPerl droplets (version 5.0.7r1m) only work if MacPerl is
also running. The droplet itself needs a fair bit of memory to convert
even the smallest file: I give it 1500k. As I come from an environment
where one always makes a backup copy of any file, the script does
likewise, rather than editing the file in place. However, in MacPerl,
`unlink' does not always remove the backup file afterwards, for some
reason.

#!/usr/bin/perl
eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
& eval 'exec /usr/bin/perl -S $argv:q'
if 0;

# 
# convert line feeds to carriage returns
#

# loop to process each file
foreach $INFILE (@ARGV)
{
	&proc_file;
}

# delete the temporary files
foreach $INFILE (@ARGV)
{
        unlink "$INFILE.old";
}
####################################################################
# process a single file
sub proc_file
{
        # rename  file
        rename($INFILE, "$INFILE.old")
                || die "\nCannot rename file $INFILE.old!\n";
        
        # open file
        open(OLD, "$INFILE.old")
                || die "\nCannot open $INFILE.old!\n";

        # create new file
        open(NEW, ">$INFILE")
                || die "\nCannot create $INFILE!\n";

        # loop to process file
        &proc_line while <OLD>;

        # set creator & type for BBEdit
        &MacPerl'SetFileInfo("R*ch", "TEXT", "$INFILE");

}
####################################################################
# process a single line
sub proc_line
{
        # convert line feed to carriage return
        s/\x0a/\x0d/g;

        # print
        print NEW $_;

}
####################################################################

Good luck.

Paul Leslie.