Synopsis 5
by Allison Randal, Damian Conway
|
Pages: 1, 2, 3, 4, 5
Editor's note: this document is out of date and remains here for historic interest. See Synopsis 5 for the current design information.
Matching against non-strings
-
Anything that can be tied to a string can be matched against a regex. This feature is particularly useful with input streams:
my @array := <$fh>; # lazy when aliased my $array is from(\@array); # tie scalar# and later...$array =~ m/pattern/; # match from stream
Grammars
- Potential ``collision'' problem with named regexes
-
Of course, a named
identregex shouldn't clobber someone else'sidentregex. - So some mechanism is needed to confine regexes to a namespace.
- If subs are the model for rules, then modules/classes are the obvious model for aggregating them.
- Such collections of rules are generally known as ``grammars''.
-
Just as a class can collect named actions together:
class Identity { method name { "Name = $.name" } method age { "Age = $.age" } method addr { "Addr = $.addr" }method desc { print .name(), "\n", .age(), "\n", .addr(), "\n"; }# etc. } -
So too a grammar can collect a set of named rules together:
grammar Identity { rule name :w { Name = (\N+) } rule age :w { Age = (\d+) } rule addr :w { Addr = (\N+) } rule desc { <name> \n <age> \n <addr> \n }# etc. } -
Like classes, grammars can inherit:
grammar Letter { rule text { <greet> <body> <close> }rule greet :w { [Hi|Hey|Yo] $to:=(\S+?) , $$}rule body { <line>+ }rule close :w { Later dude, $from:=(.+) }# etc. }grammar FormalLetter is Letter {rule greet :w { Dear $to:=(\S+?) , $$}rule close :w { Yours sincerely, $from:=(.+) }} - Inherit rule definitions (polymorphically!)
- So there's no need to respecify body, line, etc.
-
Perl 6 will come with at least one grammar predefined:
grammar Perl { # Perl's own grammarrule prog { <line>* }rule line { <decl> | <loop> | <label> [<cond>|<sideff>|;] }rule decl { <sub> | <class> | <use> }# etc. etc. } -
Hence:
given $source_code { $parsetree = m/<Perl::prog>/; }
Transliteration
- The tr/// quote-like operator now also has a subroutine form.
-
It can be given either a single
PAIR:$str =~ tr( 'A-C' => 'a-c' ); -
Or a hash (or hash ref):
$str =~ tr( {'A'=>'a', 'B'=>'b', 'C'=>'c'} ); $str =~ tr( {'A-Z'=>'a-z', '0-9'=>'A-F'} ); $str =~ tr( %mapping ); -
Or two arrays (or array refs):
$str =~ tr( ['A'..'C'], ['a'..'c'] ); $str =~ tr( @UPPER, @lower ); -
Note that the array version can map one-or-more characters to one-or-more characters:
$str =~ tr( [' ', '<', '>', '&' ], [' ', '<', '>', '&' ]);
Dr. Damian Conway is a Senior Lecturer in Computer Science and Software Engineering at Monash University (Melbourne, Australia), where he teaches object-oriented software engineering. He is an effective teacher, an accomplished writer, and the author of several popular Perl modules. He is also a semi-regular contributor to the Perl Journal. In 1998 he was the winner of the Larry Wall Award for Practical Utility for two modules (Getopt::Declare and Lingua::EN::Inflect) and in 1999 he won his second "Larry" for his Coy.pm haiku-generation module. He has just published "Object-Oriented Perl" (Manning, 1999).
Allison Randal's first geek career was as a research linguist in eastern Africa. Working with minority languages led to a series of academic papers delivered in obscure places like the Czech Republic. But eventually her love of coding seduced her away from natural languages to artificial ones. In particular, to Perl. After serving several tours of duty in the dot.com trenches, she has recently returned to Darkest Academia, at the University of Portland. In her spare time she enjoys extreme sports: teaching Perl to Java programmers, Perl Monger wrangling, and debating linguistics with Larry and Damian.
Return to Perl.com.

