The Perl Basics You Need To Know Page 6
##################################################### # Build_MatchMany_Function # -- Input: list of patterns # -- Output: A code ref which matches its sh[0] # against ANY of the patterns given in the # "Input", efficiently. # sub Build_MatchMany_Function { my @R = @_; my = join '||', map { "$_[0] =~ m/$R[sh]/o" } ( 0..0R ); my = eval "sub { }"; die "Failed in building regex @R: " if ; ; }Example usage:
@some_browsers = qw(Mozilla Lynx MSIE AmigaVoyager lwp libwww); =Build_MatchMany_Function(@some_browsers); while (<ACCESS_LOG>) { # ... = get_browser_field(sh); if ( ! &() ) { print STDERR "Unknown Browser: \n"; } # ... }In the next article I'll present a few other Perl basics directly related to the mod_perl programming.
References
-
The book Mastering Regular Expressions by Jeffery Friedl.
-
The book Programming Perl by L.Wall, T. Christiansen and J.Orwant (also known as the Camel book, named after the camel picture on the cover of the book).
-
The Exporter, perlre, perlvar, perlmod and perlmodlib man pages.
About the Author
Stas Bekman is an author of the mod_perl Guide and is currently co-athoring a book about mod_perl for O'Reilly and Associates, Inc together with Eric Cholet. He is a member of the Apache Software Foundation. You can find more about his works and joys at his personal Web site.
0 Comments (click to add your comment)
Networking Solutions
