05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

cover all of this goes beyond what is available in this book. Still, for those of you who are familiar<br />

with these or those of you looking to expand your programming repertoire, it would be<br />

remiss not to talk a bit about these languages and how they work within <strong>OS</strong> X.<br />

Each of these languages shares a number of benefits vs. more traditional compiled languages<br />

such as C, C++, Obj-C, and Java, including ease of use, portability, and immediate feedback<br />

(since there is no compilation). Since they are scripting languages, all you need to write them is a<br />

text editor, and each of these languages is free and well supported across a wide range of platforms<br />

and devices. The other side of this coin is that interpreted languages such as Perl, Python,<br />

and Ruby (and shell scripts for that matter) have a few disadvantages vs. their compiled<br />

brethren: they lack the performance of a well-optimized compiled program; they are not as well<br />

suited for low-level programming tasks; and if you choose to distribute your script, you are also<br />

revealing all your source code, which makes scripting languages less inviting for commercial<br />

applications.<br />

Perl<br />

NOTE Performance is a tricky matter. Although a well-optimized compiled program will<br />

always outperform an interpreted program (at least on the initial execution of the program),<br />

because of caching, the advanced subsequent execution of interpreted programs can easily<br />

match that of the compiled programs. Additionally, because of hardware advances, the realtime<br />

difference between the two is rapidly diminishing (though never quite reaching equality).<br />

Interestingly, certain programming technologies such as Java and some .NET technologies are<br />

both compiled and interpreted. This could lead one to assume that these would share the<br />

faults of both compiled and interpreted languages such as added development complexity<br />

and time and less than optimal performance. Although this may be true, they also gain a few<br />

benefits such as portability and source code obfuscation. (That’s not to say that if a language<br />

can be portable, it actually is.)<br />

Perl (which stands for Practical Extraction and Report Language) is currently the leading interpreted<br />

language used today (though Python and Ruby have been gaining on it over the past few<br />

years). Perl, as its name seems to imply, was initially designed to work with large chunks of text:<br />

to read in information and to parse it and/or manipulate it in meaningful ways. Thus, when the<br />

World Wide Web came into being with all of its marked-up text, Perl, combined with CGI, was<br />

uniquely suited to work with all of this data and manipulate it in fun, interesting, and useful<br />

ways. As a web language, Perl’s popularity began to grow rapidly.<br />

As a language, one of Perl’s greatest assets, and also one of its greatest weaknesses, is its fantastic<br />

flexibility. It prides itself on providing multiple ways to solve any given task—Perl’s motto<br />

is T.M.T.O.W.T.D.I. (“There’s More Then One Way To Do It”). In the hands of a skilled programmer,<br />

this flexibility can unleash wonderful things, but it can also create a lot of unintelligible,<br />

unmaintainable code (thus giving Perl the unflattering reputation as a “write-only” language).<br />

The truth is that although Perl allows you to write some very ugly code, you can also write very<br />

clean, understandable code in Perl. Most important, though, when you have a problem, Perl usually<br />

can provide a way to solve it. For example, let’s say we wanted to simplify numbering lines<br />

in source code (you know, because we’re writing a book, and sometimes it’s nice to refer to line<br />

numbers). Rather than going through each code listing and manually entering numbers, we<br />

could easy whip together the following script in Perl:<br />

#!/usr/bin/perl -w<br />

CHAPTER 19 EXTENDING THE POWER OF DARWIN 339<br />

foreach my $file ( @ARGV ) {<br />

my $n = 0;<br />

open (OFILE, $file) || die "Sorry, $file can't be opened: $!";<br />

open (NFILE, ">num_$file");

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!