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 define_group() function

The global (and thus external to the groups section parser) define_group() function takes two arguments. The first is the name of the new class that may be defined, and the second is an array reference to existing class names.

Parse::RecDescent could have done the class name validation automatically, but an external function is easier to code and understand, in my opinion.

Listing 7. The global define_group() function

# {{{ define_group: insert a new group into the list of defined groups,
#          if one of its conditionals is defined
sub define_group($$)
{
 my $group_name = shift @_ || return undef;
 my $conditional_names = shift @_ || return undef;

 foreach my $conditional (@$conditional_names)
 {
  next unless exists $classes{$conditional};
  $classes{$group_name} = 1;
  return $group_name;
 }

 return undef;
}
# }}}

Note the "next unless" control flow and the way we invoke return() on a successful match for the fastest possible traversal of the @$conditional_names array.



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

Page:  1 2 3 4 5 6 Next Page: Conclusion and Resources

First published by IBM developerWorks


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