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

It all begins with the global parser

The global parser, which I discussed in previous chapters, creates cfrun atoms. The cfrun atoms are a single configuration line (for example, "add this user") with the attached section and class-conditional execution. For instance, "add user" would be in the "users" section, and perhaps it's only for the "linux" classes.

The cfrun() function executes these cfrun atoms. Before the functional atoms are executed, however, cfrun() needs to process the import and group statements. In a future version of cfperl, the imports may be done conditionally, as in cfengine. In this version, imports are unconditional.

Listing 1. Sample import and group statements

import:
# import the file 'cf.extra'
 cf.extra


groups:

# declare the classes 'internal' and 'external' with 
# corresponding member machines
 internal = ( server1 server2 )
 external = ( server3 server4 )

# declare the class 'primary' with member 'server5'
 primary = ( server5 )

# this line will be processed only when 'primary' is defined
 primary::
# define the 'secondary' class if we're running on a Sunday
  secondary = ( Sunday )
  
Listing 2. Processing import and control/group statements in cfrun()
       
 foreach my $cfrun_atom ( grep { $_->{section} eq IMPORT_SECTION } @cfrun_queue)
 {
  dispatch(\%parsers, $cfrun_atom )
   if allowed_cfrun_atom({ section => $cfrun_atom->{section},
             classes => undef, actual => $cfrun_atom->{section} }, $cfrun_atom);
 }

 foreach my $cfrun_atom ( grep { $_->{section} eq CONTROL_SECTION ||
          $_->{section} eq GROUPS_SECTION } @cfrun_queue)
 {
  dispatch(\%parsers, $cfrun_atom )
   if allowed_cfrun_atom({ section => $cfrun_atom->{section},
             classes => undef, actual => $cfrun_atom->{section} }, $cfrun_atom);
 }

See the sample configuration cftest.conf in the cfperl CVS repository or the cfengine sample configuration (both in the Resources section) for an example of how imports work on the user side. In cfperl, imports are processed by the top-level parser with the load_file() function. Imports need to be executed first to simplify the program flow.

The control and groups sections are processed after imports but before user-side functionality atoms. The control: cfengine section defines the actionsequence and user-defined classes, for example. The actionsequence declares what the order of cfengine/cfperl sections will be. Cfperl implements the actionsequence with the cfqueue atoms, which are inserted in an array called @cfrun_order. The groups section is necessary in order to declare the classes that cfengine/cfperl will interpret for conditional execution later.

The dispatch() function takes a cfrun atom and dispatches it to the appropriate secondary parser. I'll explain the group section secondary parser and the empty default secondary parser later in this chapter.



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

Page:  1 2 3 4 5 6 Next Page: The default parser

First published by IBM developerWorks


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