macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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

Obvious Differences

The Cocoa code begins with a line of code that imports the Foundation framework.




#import <foundation/Foundation.h>

For any Perl script that contain Cocoa code, include the PerlObjCBridge with this line:


use Foundation;

Second, the Cocoa code has the main function and creates and releases the NSAutoreleasePool. You do not need any of this in Perl.

Third, you will notice that the Cocoa code uses brackets and the Perl code does not. The Perl arrow operators replace the brackets. In Cocoa, the brackets are used to show the flow of execution. For example, [[NSString alloc] initWithString:@"World"] shows that [NSString alloc] is executed first because it is the innermost code. The next code that gets executed is initWithString:@"World".

The Perl arrow operator shows the same thing. For example, NSString->alloc()->initWithCString_("World") shows that NSString->alloc() is executed before initWithCString_("World").

Fourth, Cocoa uses colons (:) and Perl uses underscores (_). Colons let Cocoa know that a parameter is expected. In Perl, those colons are replaced with underscores.

Also, in Cocoa, the name of a method consists of multiple words; a word for each parameter. Here is an example of the 10.4 stringWithCString:encoding: method (this method only works in 10.4).


[NSString stringWithCString:"Hello " encoding:NSASCIIStringEncoding]

In Perl, the words are combined into one word separated by underscores, and the parameters are listed in parentheses. So stringWithCString: is combined with encoding: to become stringWithCString_encoding_, and "Hello " and NSASCIIStringEncoding are moved into the parentheses. The order also matters. The first word and the first parameter go together.


NSString->stringWithCString_encoding_("Hello ", NSASCIIStringEncoding);

In Cocoa, you create each variable with "NSString *". You do not need to include this in Perl.

Working with Objects (Object-Oriented for Dummies, Part 1)

This section is intended for Perl programmers who do not know Cocoa. If you are comfortable with Cocoa, you may want to skim or just skip this section.


$s1 = NSString->stringWithCString_("Hello ");

This line creates an object and sets its value to "Hello ".

If you do not know what an object is, they are not that difficult to understand. Review what you already know. A variable holds a value. An array holds values that are accessed with a number index. A hash holds values that are accessed with string keywords. A subroutine is a section of reusable code that may take parameters and return a value.

An object is like a super variable. It can hold values, arrays, or hashes. What makes it even more super is that objects can also contain subroutines, or methods.

Take the NSString object. It holds one value, a string. It also contains methods. The method stringByAppendingString_() takes a parameter and it returns a value, just like any subroutine. Where is the stringByAppendingString_() code? It is located in the NSString object.

Methods usually do something with the value stored in the object. The method stringByAppendingString_() returns a new string that is a combination of the string in the object and the string in the parameter. The methods stringWithCString_() and initWithCString_() set the object's current value, but each is slightly different. In all cases, the methods deal with the object's value.

When creating objects, you always give the name of the object you are creating, followed by a creation method, called a "factory" or "class" method. Look at the NSString documentation and scroll down to the section titled "Method Types." Some methods are listed with a plus (+) before them, and some with a minus (-).

When creating an object, you must use a method with a plus. You only call those methods once for an object. Here is a list of methods that can create NSString objects:


+ stringWithFormat:
+ localizedStringWithFormat:
+ stringWithCharacters:length:
+ string
+ stringWithString:
+ stringWithCString:encoding:
+ stringWithUTF8String:
+ stringWithContentsOfFile:encoding:error:
+ stringWithContentsOfFile:usedEncoding:error:
+ stringWithContentsOfURL:encoding:error:
+ stringWithContentsOfURL:usedEncoding:error:

The alloc() method is also used to create an object. The difference between alloc() and the other creation methods is that alloc() requires you to call an init-type method immediately afterwards. The other creation methods do not need an init-type method called.

The other unique aspect about creating objects with alloc() is that you should release() the objects when you are done using them, or you will leak memory. If the object is only used once and the script performs only a few tasks and then exits, it is not serious if you do not release it. But if your script runs for a long time, or you use alloc() inside of a loop, you certainly want to release the objects when you are done with them, or your script will start using huge amounts of RAM. Here is a simplified example of creating and releasing an object with alloc() and release().


for ( $x=1; $x<100000; $x++ ) {
	$y=NSString->alloc()->initWithCString_("$x");
	# use $y here... release when you are done:
	$y->release();
}

If you did not release, then your script would take more memory than it needs. To play it safe, you could just avoid using alloc.

Once you create the object, you only want to call the methods listed with a minus; these are called "instance" methods.

Now let us look closely at the example.


$s1 = NSString->stringWithCString_("Hello ");
$s2 = NSString->alloc()->initWithCString_("World");
$s3 = $s1->stringByAppendingString_($s2);
printf "%s\n", $s3->UTF8String();

$s2->release();

The first two lines create an NSString, first using NSString->stringWithCString_(), and second, using NSString->alloc()->initWithCString_(). The object pointed to by $s2 is created with alloc(), so we release it when we are done using it.

The third way to create an object is used when you have an object that returns a new object. The third line in the code example shows this:


$s1->stringByAppendingString_($s2)

The result of this is a brand new NSString object. The method stringByAppendingString_() is just like any subroutine or function; it takes parameters and returns a value. The value just happens to be a reference to a newly created NSString object. The full definition on the NSString documentation page is:


- (NSString *)stringByAppendingString:(NSString *)aString

The first word at the beginning tells what the method returns: "(NSString *)", or an NSString object location. The asterisk means it is a pointer, or reference. So the value returned is a reference to a brand new NSString object.

I can then store the location of this new object in $s3.


$s3 = $s1->stringByAppendingString_($s2);

You will notice that $s3 was not alloced, and stringByAppendingString_() is an instance method (listed with a minus). Because stringByAppendingString_() created an object, you don't have to use alloc() or another factory or class method with $s3. But notice that $s1 was already created. You can't call stringByAppendingString_() on an object that hasn't been created.

Pages: 1, 2, 3, 4, 5

Next Pagearrow