in reply to Manipulating Binary files

Being paranoid about really big files, I'd probably do:

{ local $/= \4096; binmode(INPUT); binmode(OUTPUT); while( <INPUT> ) { if( s/^.*?(\x0d\x0a)/$1/s ) { print OUTPUT $_; last; } } print OUTPUT $_ while <INPUT>; }
But setting $/ to be a reference to a block size is a recently added feature so be aware that your version of Perl may not support it yet. In which case you can change <INPUT> to:     read(INPUT,$_,4096)

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
(Ovid) Re: (tye)Re: Manipulating Binary files
by Ovid (Cardinal) on May 14, 2001 at 20:16 UTC
    For a one-shot program, I'd be happy to use your code. However, if one is likely to use this repeatedly (which it doesn't sound like), then there is a potential bug. What happens if 0\x0D is the 4,096th character and 0x0A is the 4,097th? That would be annoying to track down (ain't boundaries a pain?).

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Oops.

      { binmode(INPUT); binmode(OUTPUT); local $_= ""; while( read( INPUT, $_, 4096, length($_) ) ) { if( s/^.*?(\x0d\x0a)/$1/s ) { print OUTPUT $_; last; } substr( $_, 0, -1 )= ""; } print OUTPUT $_ while read INPUT, $_, 4096; }
      Thanks for catching that.

              - tye (but my friends call me "Tye")