10.1 Perl for Mac OS X Geeks

The following sections list a few of the extras that either come with Mac OS X or can be easily installed.

10.1.1 Mac::Carbon

This module comes by way of MacPerl (http://www.macperl.org), a distribution of Perl for Mac OS 9 and earlier. Mac::Carbon (available from the Download link on the MacPerl web site) gives Perl programmers access to the Carbon APIs. Its test suite is great; make sure you have your speaker volume turned up when you run it. One of the many modules included with Mac::Carbon is MacPerl; here's an example that pops up a dialog box and asks a question:

 #!/usr/bin/perl -w use MacPerl qw(:all); $answer = MacPerl::Ask("Tell me how good you thought my poem was."); if ($answer =~     /counterpoint the surrealism of the underlying metaphor/i) {   $die_in_the_vacuum_of_space = 1; } 

You can install Mac::Carbon with the CPAN shell, described later in this chapter. After it's installed, you can read the documentation with perldoc Mac::Carbon .

10.1.2 PerlObjCBridge.pm

This module ships along with Mac OS X, and it gives you a way to call into the Objective-C runtime on Mac OS X. Given an Objective-C call of the form:

 Type x = [Class method1:arg1 method2:arg2]; 

you can use the equivalent Perl code:

 $x = Class->method1_method2_($arg1, $arg2); 

You could also create an NSString and display it with the following script:

 #!/usr/bin/perl -w use strict; use Foundation; # import Foundation objects my $string = NSString->stringWithCString_("Hello, World"); print $string, "\n";      # prints NSCFString=SCALAR(0x858398) print $string->cString( ); # prints Hello, World 

You can read the documentation for this module with perldoc PerlObjCBridge .

10.1.3 Mac::Glue

This module lets you invoke Apple Events from Perl. To use it with an application, you'll need to create a layer of glue between this module with the gluemac utility, which is installed along with Mac::Glue. For example, to create the glue for the Terminal application, do the following:

 $  sudo gluemac  /Applications/Utilities/Terminal.app/  Password:  ********  What is the glue name? [Terminal]:  Created and installed App glue for Terminal.app, v1.4.1 (Terminal) 

This also creates documentation for the module. To read it, use perldoc Mac::Glue::glues:: appname , as in perldoc Mac::Glue::glues::Terminal .

Here's a short example that uses the Terminal glue to open a telnet session to the Weather Underground :

 #!/usr/bin/perl -w use strict; use Mac::Glue; my $terminal = new Mac::Glue 'Terminal'; $terminal->geturl("telnet://rainmaker.wunderground.com"); 

You can install Mac::Glue with the CPAN shell (described later).

There are many complicated dependencies; we found that Mac::Glue failed the first time through on one of the dependencies, and again the second time we tried to install it. We quit the CPAN shell, restarted it, and tried the install a third time ”that was the charm . When you install Mac::Glue, pay close attention to the list of glues it creates, so you don't end up recreating them unnecessarily. When we installed Mac::Glue, this was its output:

 Created and installed Dialect glue for AppleScript.rsrc    (AppleScript) Created and installed Addition glue for Digital Hub Scripting.osax    (Digital_Hub_Scripting) Created and installed Addition glue for StandardAdditions.osax    (StandardAdditions) Created and installed App glue for Image Capture Scripting.app    (Image_Capture_Scripting) Created and installed App glue for Finder.app, v10.3    (Finder) Created and installed App glue for Keychain Scripting.app    (Keychain_Scripting) Created and installed App glue for System Events.app, v1.2.1    (System_Events) Created and installed App glue for ColorSyncScripting.app    (ColorSyncScripting) Created and installed App glue for URL Access Scripting.app    (URL_Access_Scripting) Created and installed App glue for FontSyncScripting.app, v2.0d1    (FontSyncScripting) 


Mac OS X Panther for Unix Geeks
Mac OS X Panther for Unix Geeks
ISBN: 0596006071
EAN: 2147483647
Year: 2003
Pages: 212

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net