Perl's Natural Language Features:

does flexibility lead to ambiguity?

Here's an example of an English sentence to keep you interested…


400-year-old spoiler: someone defeats him.
Don't give away the answer if you know it!

  • Perl has some unusual features
  • Those features imitate human languages
  • Larry is a linguist, this isn’t a coincidence

Sigils

$ @ and % — could & be considered a sigil?

Sigils imitate human language

Look out of the window, what do you see?

  • A cow
  • cows

That applies to scalars and arrays … English doesn't have a direct match to hashes, although many languages have a 'dual' form specifically for two instances of things.

Language Single instance Exactly two instances Three or more
English: Sea n/a Seas
Arabic: Bahar Baharain Baharaat

Sigils began in BASIC where they were originally on the right

Other languages have them too

PHP uses the dollar sign, so that you know $something is … something, but you don't know what.

Hungarian notion, artificial sigils for languages which don’t have them:

Pronouns

Pronouns in English are substitutes for the full names of things

  • Do you know Jane the programmer? Yes, I know her.
  • Do you know “Smells Like Teen Spirit” by Nirvana? Yes I know it
  • Do you know the Sydney.pm group? Yes, I know them.

Rather than say the whole name again, the pronoun stands in for the whole name.

In Perl, the equivalent of this is $_, and in a plural form @_

while reading each line in a file, print it

for each item in an array, print it

for each key in a hash add its value

Perl actually goes further, you can even skip the default pronoun:

print foreach @array;

—is there an equivalent of this in English?

“Unnecessary” Words

Perl has a number of words which aren’t strictly necessary,

unless, which is the opposite of if, and until, which is the opposite of while.

They are “unnecessary” because you can always write if(! $foo )

So why the extra words? Only because it makes it easier to write and to understand.

Show of hands—Do you use "unless" or "until"?

Word order and TIMTOWTDI

In perl we can write with different word orders and still achieve the same thing, just as you can in English

print $foo if $bar;
if ($bar) {print $foo;}
or even
$bar && print $foo;

Ambiguity

If a language is flexible, does that make it easier for ambiguity to happen?

Classic examples in English:

“See that nail? When I nod my head, hit it”

and, less hilariously:

“Let him have it, Chris”

“Let him have it, Chris” was said by this gentleman in 1952:

and the UK legal and political establishment were still arguing over it in 1998 …

… long after his death by hanging.

https://en.wikipedia.org/wiki/Derek_Bentley_case

Does this flexibility make it easier to write an ambiguous statement in Perl?

Some examples in perltrap and perlop

note that

print ($foo & 255) + 1, "\n";

probably doesn't do what you expect at first glance.

Personally, I’ve got caught before with or and || which mean the same thing in one sense but not another and have very different contexts and precedence.

print FILEHANDLE $foo or die;

is very different to

print FILEHANDLE $foo || die;

Perl's style guide:

$ perldoc perlstyle

Just because you can write very terse perl, doesn’t mean you should.

Less widely-used features of Perl:

Loop labels (almost like an executable comment):

LINE: for (;;) { statements; last LINE if $foo; next LINE if /^#/; statements; }

One way Perl can be made really like English is…

use English;

Short name English pragma name
$_ $ARG
$" $LIST_SEPARATOR
$/ $INPUT_RECORD_SEPARATOR
$& $MATCH
$@ $EVAL_ERROR
$$ $PROCESS_ID

Other languages which have similar "natural language" features

  • Python is supposed to be “executable pseudocode”, but, really?
  • AppleScript is the only current language which really tries to act like human language
  • copy the third word of the text of window 1 of application "BBEdit" as string to myVarName

A now defunct language which really was like executable pseudocode:

on mouseUp answer file "which file?" put it into theFilename if there is a file theFilename then put 1 into theNumber open file theFilename repeat forever read from file theFilename until return put it into currentLine if currentLine is empty then exit repeat end if delete last character of currentLine answer "Line " & theNumber & " contains " & quote & currentLine & quote add 1 to theNumber end repeat close file theFilename else answer "there is no file by that name" end if end mouseup

Anyone know any others?


Shakespeare's "Macbeth"

So … who defeats him and how?

MacDuff defeats him:

Despair thy charm
And let the angel whom thou has served
Tell thee Macduff was from his mother’s womb
Untimely ripped.
(5.10.14-16)

MacDuff technically wasn't born, but delivered by Caesarian Section.

One last thing…

Community.

Community

Larry Wall didn't just design a language.

He designed a community and a culture around that language.

A language is not a set of syntax rules. It is not just a set of semantics. It's the entire culture surrounding the language itself. So part of the cultural context in which you analyze a language includes all the personalities and people involved.

So that's where we come in.

/