7.1 Using Rules

     

Rules are a language within a language, with their own syntax and conventions. At the highest level, though, they're just another set of Perl constructs. So the first thing to learn about rules is the Perl "glue" code for creating and using them.

7.1.1 Immediate Matches

The simplest way to create and use a rule is an immediate match. A rule defined with the m// operator always immediately matches. Substitutions, defined with the s/// operator also immediately match. A rule defined with the // operator immediately matches when it's in void, Boolean, string, or numeric context, or the argument of the smart-match operator ( ~~ ).

 if ($string ~~ m/\w+/)      { . . . } if ($string ~~ s/\w+/word/) { . . . } if ($string ~~ /\w+/)       { . . . } 

You can substitute other delimiters, such as # . . . # , [ . . . ] , and { . . . } , for the standard / . . . / , though ? . . . ? and ( . . . ) are not valid delimiters:

 if ($string ~~ s[\w+][word]) { . . . } 

7.1.2 Deferred Matches

Sometimes you want a little more flexibility than an immediate match. The rx// operator defines an anonymous rule that can be executed later.

 $digits = rx/\d+/; 

The simple // operator also defines an anonymous rule in all contexts other than void, Boolean, string, or numeric, or as an argument of ~~ :

 $digits = /\d+/; # store rule 

You can use the unary context forcing operators, + , ? , and ~ , to force the // operator to match immediately in a context where it ordinarily wouldn't. For a Boolean value of success or failure, force Boolean context with ?// . For a count of matches, force numeric context with +// . For the matched string value, force string context with ~// .

 $truth  = ?/\d+/;       # match $_ and return success $count  = +/(\d+\s+)*/; # match $_ and return count $string = ~/^\w+/;      # match $_ and return string 

Another option for deferred matches is a rule block. The rule keyword defines a named or anonymous rule, in much the same way that sub declares a subroutine or method declares a method. But the code within the block of a rule is rule syntax, not Perl syntax.

 $digits = rule {\d+}; rule digits {\d+} 

To match a named or anonymous rule, call it as a subrule within another rule. Subrules, whether they're named rules or a variable containing an anonymous rule, are enclosed in assertion delimiters < . . . > . You can read more about assertions in Section 7.2.4 later in this chapter.

 $string ~~ /\d+/; # same as $string ~~ /<$digits>/; $string ~~ /<digits>/; 

Table 7-1 summarizes the basic Perl syntax for defining rules.

Table 7-1. Rules

Syntax

Meaning

m/ . . . /

Match a pattern (immediate execution).

s/ . . . / . . . /

Perform a substitution (immediate execution).

rx/ . . . /

Define an anonymous rule (deferred execution).

/ . . . /

Immediately match or define an anonymous rule, depending on the context.

rule { . . . }

Define an anonymous rule.

rule name { . . . }

Define a named rule.


7.1.3 Grammars

A grammar is a collection of rules, in much the same way that a class is a collection of methods . In fact, grammars are classes, they're just classes that inherit from the universal base class Rule . This means that grammars can inherit from other grammars, and that they define a namespace for their rules.

 grammar Hitchhikers {     rule name {ZaphodFordArthur}     rule id   {\d+}      . . .  } 

Any rule in the current grammar or in one of its parents can be called directly, but a rule from an external grammar needs to have its package specified:

 if $newsrelease ~~ /<Hitchhikers.name>/ {     send_alert(); } 

Grammars are especially useful for complex text or data parsing.



Perl 6 and Parrot Essentials
Perl 6 and Parrot Essentials, Second Edition
ISBN: 059600737X
EAN: 2147483647
Year: 2003
Pages: 116

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