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

Re: [MacPerl] Another elementary question



At 18.46 -0500 1999.03.12, Rich Morin wrote:
>At 12:05 PM -0800 3/12/99, Ronald J. Kimball wrote:
>>Replace the unwanted newlines before giving the file to your script.
>
>That gets me wondering.  What if he doesn't want to have to run two
>scripts?  What's the easiest way to perform
>
>  cr_filter < infile | analysis_program
>
>under MacPerl?  Winners, if any, by concensus...

Depends.  Do you know what is in the file?  If you know you are on a Mac,
and the file is either Mac, DOS, or Unix format, you can do:

    sub get_text {
        my $file = shift;
        local($/, *F);
        open F, "< $file\0" or die $!;
        (my $text = <F>) =~ s/\015?\012/\n/g;
        $text;
    }

If you are not sure what platform you will be on, then you need to do
something else.  It should work, I think, for DOS, Unix, or Mac files, on
DOS, Unix, or Mac platforms.

    sub get_text {
        my($file, $c, $text) = (shift, 0);
        local($/, *F);
        open F, "< $file\0" or die $!;
        $text = <F>;
        until ($/) {
            seek(F, $c++, 0);
            read(F, my $b, 1);
            if ($b eq "\012") {  # is Unix
                $/ = $b;
            } elsif ($b eq "\015") { # is DOS or Mac
                seek(F, $c++, 0);
                read(F, my $b, 2);
                $b =~ /^(\015\012?)/;
                $/ = $1;
            }
        }
        $text =~ s|$/|\n|g unless $/ eq "\n";
        $text;
    }

I did not test any of the above code.  :)

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

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