• Aucun résultat trouvé

A VISIT TO P ERLISTAN

Dans le document Minimal Perl (Page 36-40)

Introducing Minimal Perl

1.1 A VISIT TO P ERLISTAN

1.2 Perl can be simple 7 1.3 About Minimal Perl 7

1.4 Laziness is a virtue 9 1.5 A minimal dose of syntax 10 1.6 Writing one-line programs 11 1.7 Summary 14

Perl is a great language, with an ingenious mentality underlying it, and a terrific user community gathered around it. Given these properties, it’s no wonder it has become so popular.

But Perl is also a large and complex language that offers the programmer many dif-ferent ways to accomplish the same goal. This can make it hard for beginners to learn the language and for one programmer to understand a program written by another.

In this chapter, we’ll discuss the motivation behind the development of Minimal Perl and the philosophy underlying the special subset of the standard Perl language that it employs. But before delving into those matters, we’ll begin our journey with a tale of travel that provides a helpful orientation to Perl culture.

1.1 A

VISIT TO

P

ERLISTAN

A Unix user visiting the world of Perl may feel like he’s in a foreign land, where people use words that sound familiar but that have different meanings than they do back home. Let’s observe the adventures of such a Traveler, as he tries to understand the way people communicate in Perlistan.

Scene: The main market of Perlistan, a hub of the famous Silk Road, in a high desert region of central Asia. Nomads in tribal dress are haggling enthusiastically for discounts on goods, as camel carts and yak carts compete for

space in the narrow lanes of the old city.

As the Traveler pauses to snap a photo of an ornately decorated camel, he overhears a merchant saying to his assistant

“Foreach gingko nut in basket, if it passes freshness test, push it in bag.”

The assistant complies, and hands the waiting customer a bag of fresh nuts from the Gingko Biloba tree.

The Traveler can’t help but notice the distinctive triangle drawn on the merchant’s forehead. He’s heard that these markings hold a special significance in Perlistani com-munications, so he makes a mental note of this transaction for later reference.

Across the way, he observes another merchant, also wearing a triangle, getting the same result by telling her identically marked assistant something a bit different:

“Test it for freshness and push it in bag, foreach gingko nut in basket.”

It disturbs the Traveler that this instruction was expressed backwards relative to that of the first merchant and that and is now playing the conditional role previously played by if. He wonders if it’s significant that this merchant’s triangle points to the left (from the customer’s viewpoint), whereas that of the first merchant points to the right.

Next, he overhears a merchant bearing a large circle on his forehead saying to his identically marked assistant

“Bag—grep ripe—basket of kiwis.”

The Traveler knows that grep is used back home for selecting items on the basis of matches, so he suddenly fells hopeful that he may be able to crack this linguistic code after all. But now he’s wondering why the two “triangled” merchants expressed a related request in a completely different way—without any reference to grep!

Then he hears a fourth merchant, also distinguished by a forehead circle, call out:

“Bag—grep rand—basket of jerkies.”

An apparently random selection of assorted jerkies (dried meats) is put into the bag by his circle-wearing assistant.

Now the Traveler is perplexed. His theory about grep being for matching—

which fit the grep ripe case perfectly—is discredited by this new evidence. That’s because rand, unlike ripe, wasn’t a property of any of the selected jerkies and therefore shouldn’t have been usable as a basis for matching. At least, the grep back home doesn’t work like that!

Seeking consolation, he buys a few assorted jerkies and commences to gnaw con-tentedly. A runny-nosed boy interrupts his reverie, offering to change money for him at attractive black-market rates. The Traveler declines, but not before learning

that the jerky with the rigid texture and overbearing flavor is made from dried python meat.

Then he notices a merchant sporting a square on his forehead, who is accumulat-ing a long queue of impatient customers. Shortly after the merchant finishes intonaccumulat-ing the following long-winded set of instructions to his (equally square) assistant, a dis-gruntled customer finally receives a bag of ripe figs:

“ Set variable candidate to 0;

set variable selection to 0;

test variable candidate is less than number of figs in basket or finish;

select fig from basket;

if fig passes test for ripeness, mark it with value of variable selection, increment variable selection by 1, and put fig into bag;

increment variable candidate by 1;

repeat from ‘test variable candidate’ step.”

Now the Traveler is mystified. Given the ample evidence that there are more concise ways to express this kind of transaction, why would anyone go to all the trouble to do it this way? Could there be some advantage to this excessively verbose manner of speaking—like, do they get paid by the syllable? Or maybe these squared guys are just meticulous to a fault.

At this point, the Traveler wonders how any visitor could pick up the language of Perlistan, given its many different—and largely unrelated—ways of expressing the same idea. His chances of mastering its nuances seem as remote as those of him being invited to a high tea, catered by Oxford-educated snow leopards, at the den of the Alpha Yeti.1

In frustration, he rushes to catch the quickest flight home, and he resolves that his next vacation will be free of linguistic puzzles—and python jerky!

But the Traveler is left wondering if he could have had a better experience in Per-listan, if only he had tried a different approach.

1.1.1 Sometimes you need a professional guide

It’s too bad linguistic turmoil ruined the Traveler’s vacation. He should have hired a certified guide before venturing into Perlistan’s market district—as the brochure had advised. Had he done so, he would have learned that the JAPHs (which is what the people of Perlistan call themselves) do indeed share a common language called Perl, which was invented with the founding of the country in 1987.

Rather than being a new creation of whole cloth, Perl was created like a patchwork quilt, incorporating the best features of the languages that were the mother tongues of the JAPHly tribes. But it’s not an ugly quilt with a jumble of patterns and shapes

1 Yeti is the Perlistani name for what other cultures call bigfoot or the abominable snowman.

all forced together, as you might expect. Instead, thanks to the ingenuity of Perl creator Larry Wall (“Larry”), it’s a work of art that cleverly coordinates its disparate elements into an aesthetically pleasing result.2

Naturally, JAPHs tend to feel most comfortable using the elements of Perl that are derived from their own mother tongues. It shouldn’t be surprising therefore that some of these makeshift dialects are so dissimilar as to appear to be different languages because, in fact, they started out that way!

Now you know why the Perlistanis wear dialect-identifying markings—it’s so they can easily identify others from their linguistic group.

Table 1.1 summarizes the meanings of the different forehead markings and indi-cates the subculture associated with each dialect.

With the benefit of this table, we’re now equipped to understand the Trav-eler’s observations.

The triangles identify those whose mother tongue is Shell and who find Perl’s flow-control structures to be refreshingly familiar. These JAPHs come in two varieties, so it’s important to notice the direction in which the triangle is pointing. If it points to your right, the individual follows the time-honored tradition of putting control keywords such as if and foreach at the beginning of the sentence. However, Perl also allows such instructions to be expressed in backward order, which those wearing left-pointing triangles consider to be more natural.

Those wearing circles have lots of spare time left over after communicating their needs to each other, but they have fewer friends to enjoy it with. That’s because they either learned Perl as their mother tongue or have lived in Perlistan long enough to lose their native accents, and they now speak idiomatic Perl—which doesn’t have much in common with its UNIX-based predecessors.

Those wearing the mark of the square need great patience, because it takes them a long time to communicate. Their dialect emphasizes features derived from the

2 Others have commented on the eclectic mix of linguistic ingredients that constitute Perl, such as Yoz Grahame, who wrote an interesting article called “Perl is Internet Yiddish” (available on the Internet).

Table 1.1 Forehead markings for the Perl dialects of Perlistan

Forehead marking Perl dialect Marketplace quote

Right-pointing triangle UNIX Shell Foreach gingko nut in basket, if it passes freshness test, push it in bag.

Left-pointing triangle UNIX Shell (but backward)

Test it for freshness and push it in bag, foreach gingko nut in basket.

Circle Idiomatic Perl Bag—grep ripe—basket of kiwis.

Bag—grep rand—basket of jerkies.

Square C language set variable candidate to 0; …

notoriously hazardous C language, which breeds a mistrust among its users that causes them to compulsively over-specify all aspects of every operation.

But Perl doesn’t have to be this complicated.

Dans le document Minimal Perl (Page 36-40)