oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Using Perl to Manage Plist Files
Pages: 1, 2, 3, 4, 5

Pointers and References (Object-Oriented for Dummies, Part 2)

Once you create your objects, the variables $s1, $s2, and $s3 store the locations of the objects. These variables are called pointers in Cocoa and are converted to references in Perl. Pointers and references cause endless confusion to new programmers. There are a few things to remember about them.

First, you have to talk about them differently. Instead of saying "modify the object $s1," you have to say "modify the object referenced by $s1." Sometimes it is easier to say the former, but this is inaccurate, because $s1 isn't the object. I only mention this because in this article, I'll often say, "The object referenced by." It is wordy, but more accurate.

However, if you are new to pointers and references, there wont be any serious harm to think or talk of $s1 as the object. In time, you will want to make the mental shift, but don't fret over it. Failure to make the mental shift isn't worth not using the code. Continue to think of $s1 as the object for now. Not much behavior really changes by $s1 being a pointer instead of the object.

Second, you do not directly access $s1 and $s2. These are all wrong:

$s3 = $s1 . "World";


$s3 = $s1 . $s2;


print $s3


print "s3 is $s3";

To interact with an object, you always use a method. For example, to combine $s1 and $s2, I used:


If I wanted to print $s3, I might try:

print $s3;

But I would get something like:


Oops! That is the memory location of the object! Instead, I can use the method UTF8String():

print $s3->UTF8String() . "\n";

Note that because UTF8String() does not take any parameters, it does not have an underscore.

Load a Plist File

What exactly is a Plist file? You may think it is an XML file, but that is not true, especially now that many 10.4 Plist files are saved in binary format; not XML at all. Plist files store different data types. They can have two container data types: array and dictionary. A dictionary is the same as a Perl hash. The base container of a Plist file is almost always a dictionary.

Both arrays and dictionaries can contain mixed data types such as string, int, float, boolean, date, data, or even a nested array or dictionary. These data types match the Cocoa objects NSDictionary, NSArray, NSString, NSNumber, NSDate, and NSData.

If you desire more information about Plist files, you can read Apple's Plist documentation.

When the base class of a Plist file is a dictionary, the base Cocoa class will be NSDictionary. It is possible to have an NSArray as the base class, but I have not noticed any Plist files with that base class.

Conveniently for us, NSDictionary has a method that loads a Plist file from disk.

$file = "/Library/Preferences/SystemConfiguration/preferences.plist";
$plist = NSDictionary->dictionaryWithContentsOfFile_( $file );

The method dictionaryWithContentsOfFile_() is a factory or class method, so you can use it to create the object. The object will be filled with the contents of the file /Library/Preferences/SystemConfiguration/preferences.plist.

If you look at the documentation for dictionaryWithContentsOfFile_(), you will notice it takes an NSString. But we are passing it a Perl string. For convenience, a Perl scalar variable is converted to an NSString object when the method expects an NSString object.

The file you load must be a valid Plist file or else $plist will be set to the nil object. If there is a Cocoa error, then $plist will be undefined. You should check the value of $plist right after creating it just to make sure it has loaded. First, test to see if there was a Cocoa error with if ( $plist ). Then test for nil. The nil value is not empty or set to 0. So if ( $plist ) will be true if $plist is nil. To test for the nil object, use if ( $$plist ). When $plist is nil, this test will be false. Here is an example showing both tests:


use Foundation;

$file = "/bad/path.plist";
$plist = NSDictionary->dictionaryWithContentsOfFile_( $file );
if ( ! $plist or ! $$plist ) {
  print "One of the following failed: $plist - $$plist\n";

Pages: 1, 2, 3, 4, 5

Next Pagearrow