Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PERL TUTORIALS > THE ROAD TO BETTER PROGRAMMING: CHAPTER 9. THE CLASSES AND DEFAULT PARSERS


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

The road to better programming: Chapter 9. The classes and default parsers
By Teodor Zlatanov - 2004-03-29 Page:  1 2 3 4 5 6

The group section parser

The parser for the groups: section will define classes based on other classes' existence. For instance, "a = ( b )" will define "a" only if "b" has already been defined. The group-based class definition abilities of cfengine/cfperl are very powerful.

Listing 5. The group section parser
         
$parsers{GROUPS_SECTION()} = new Parse::RecDescent (q{
  input: define_group

  define_group: class '=' '(' class_definition ')'
              { ::define_group($item{class},  $item{class_definition}); 1; }

  class_definition: class(s)

  class: word
  word: /\w+/

});

As with all other cfperl parsers, this one has an input() method. Here, input() can only do one thing: define groups. Remember that with the Parse::RecDescent module, all rules are also methods, which is why we regard input() as a method.

The define_group rule is based on the class and class_definition rules. The class can only be a word made up of what Perl considers word (\w) characters, but we can change that definition if we have to. For instance, we could have "word: /[\w-]+/" as a rule if we wanted hyphens to also be valid class characters.

The class_definition rule is made up of at least one class name. Thus, "groupname = ()" would not be a valid line in the groups: section in cfperl. If we wanted to allow that, we could use "class_definition: class(s?)" in the rule. But we don't, because it makes no sense to have that.

The define_group rule uses class and class_definition to build a group definition action. It is more liberal than cfengine's group definition rule, which requires that the parentheses be surrounded with space, so "groupname = (linux)" is invalid. Cfperl will parse that line happily. If you wanted to force cfengine's behavior, you could use the following:

Listing 6. The stricter define_group rule


  define_group: class '=' '(' /\s+/ class_definition /\s+/ ')'
               { ::define_group($item{class},  $item{class_definition}); 1; }

That stricter behavior, however, is unnecessary in my opinion.

The define_group rule uses the Parse::RecDescent data structures to build the two parameters to the external define_group() function. Note that the external define_group() function is global, as denoted by the two colons before its name.



View The road to better programming: Chapter 9. The classes and default parsers Discussion

Page:  1 2 3 4 5 6 Next Page: The define_group() function

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.