Reading and writing hex data from file with Perl

4.3k Views Asked by At

I have a Perl script that creates files, and I want to write into their resource forks, in order to specify the default application they should be opened with (I can't just change it using get information in Finder, it keeps reverting).

I could find out about Apple's arcane resource fork stuff, but for my purposes I'm happy to read the data from an existing resource fork that opens with the right application, and hard-code it into my script. The trouble is, I'm too new to Perl to know how to read the hex numbers from the file, copy and paste it into my script, and have it written correctly as hex into my new .rsrc files.

I want to do something like:

my $theHex="DEADBEEF" #<---this is where I would paste the hex from the existing rsrc file
open (OUT, ">$filename/rsrc"); #<--this is the resource fork for file $filename
printf OUT "%x" $theHex #<----I'm not sure that this formatting is right

Edit: Yes I know, resource forks are deprecated. Please tell that to Apple so they can stop using them in their OS to determine things like what application opens a particular file. Sheesh!

2

There are 2 best solutions below

7
On BEST ANSWER

You have a couple problems in that code. First of all, you need a comma after your format string:

printf OUT "%x", $theHex

Now that we have some syntactically valid source code, we can move on to your real problem. The %x format string for printf expects to be given an integer but you're giving it a non-numeric string. When the string value of $theHex is converted to an integer, you'll get zero. The fix for this is easy, you just need to properly initialize $theHex:

my $theHex = 0xDEADBEEF;

The "0x" prefix tells Perl that you're writing the number as hex; similarly, a zero prefix (e.g. 0666) indicates octal.

Now you have a hexadecimal string representation of your 0xDEADBEEF integer.

If you just want to copy four bytes from one resource fork to another, then you're better off using read to read four raw bytes and then use print to write those bytes to the other resource fork.

You might want to have a look at pack if you're slinging bags'o'bytes around. AFAIK, the resource fork wants four bytes rather than an eight byte human readable hexadecimal representation of a four byte integer.

3
On

Mac OS X still has resource forks, but they've been deprecated for years. Resources don't play well with other operating systems. This tended to make Macs not play well with others and thus isolated Macs into little islands by themselves.

Resource forks were mainly used with executables, but a Mac OS X application is really a folder (aka bundle) that contains the resources inside of it.

Data files could have resource forks, but most did not. If they did, it was mainly to implement the APPL/TYPE resources to say what the file type was and the application to open it. Now, Mac OS X uses file suffixes. It's not as neat as resource forks, but it's a lot more compatible with other applications.

However, if you really want to futz with resource forks and other Mac oddements, take a look at CPAN::Mac-Carbon. That'll give you what you need.

NOTE!!

This is Mac-Carbon which means this will go bye-bye with Lion. Much of this is from the old System 6/7/8/9 days before Mac OS X, and some of this Mac OS X retained for compatibility purposes.