forrest has asked for the wisdom of the Perl Monks concerning the following question:

Heya fellow monks!

I need to perform some simple batch edits on about a gazillion Macintosh EPS files. That's easy with perl :-)

The catch is, I need to preserve the PICT previews for these files. I'm no mac guru, but it's my understanding that the preview is stored in the resource fork and the actual data I need to edit is in the data fork of that whacky Mac bipartite file format.

How can I edit the data while preserving the previews?

The files reside on Mac OS 9, so I either have to set up perl there, or transfer the files to another environment (e.g. linux) and somehow get them back intact.

Any assistance would be exceedingly appreciated.

  • Comment on Edit Macintosh file, keeping resource fork

Replies are listed 'Best First'.
Re: Edit Macintosh file, keeping resource fork
by eieio (Pilgrim) on Feb 10, 2005 at 21:11 UTC
    If you edit the file in-place (e.g., open( FILE, "+<", $fileName );), the resource fork will be preserved while you edit the data fork. However, I'm not sure if your batch edits would work well with this approach. If you have access to a Mac OS X system, perl is already installed. Otherwise, installing MacPerl is straightforward.
      Thanks for the tip!

      The type of editing I want to do is a simple line-by-line

      while (<IN>) { s/foo/bar/g; print OUT; }
      type thingie. That code is written to make a copy as it edits (which is what I've always done).

      How would I do that with in-place editing? Is it possible?

        If you are not increasing or decreasing the number of characters in the line, the following example may work:
        open( FILE, "+<", $fileName ) or die "can't open file: $!"; while( <FILE> ) { if( s/foo/bar/g ) { seek( FILE, -(length), 1 ) or die "can't seek: $!"; print FILE; } }

        Alternatively, you could read the entire file into a variable, perform your substitutions, seek to the beginning of the file, and write the contents of the variable to the file.