CGI::Search

Simple way of using a CGI to search flat-text database files
Download

CGI::Search Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Publisher Name:
  • Timm Murray
  • Publisher web site:
  • http://search.cpan.org/~tmurray/

CGI::Search Tags


CGI::Search Description

Simple way of using a CGI to search flat-text database files Many CGIs simply search flat-text databases and return the results to the browser. CGI::Search implements a generic interface for searching such databases and returning an HTML::Template object ready to be presented to the browser. Returning the data as a hash-of-arrays is also possible, although this will probably be less used in practice.Input from the user and the database is easily verified, thus making it simple to write secure and more robust code.An Object Oreinted Purist will note that two orthogonal concepts (searching and user input validation) are being put together. In this case, I have rejected the purist approach because binding these two together will make it so easy to do validation that there is no excuse for not doing it. If the purists want to use a different module (and probably write twice as much code doing it), that is fine with me.SYNOPSIS use CGI::Search qw(:validators); use CGI qw(:standard); my $TMPL_FILE = '/path/to/template'; my $DB_FILE = '/path/to/flat_file'; my $DB_SEPERATOR = '\|'; # Be sure to escape any special regex chars and put in single-quotes # This is not a good validator. Don't do this. my $CUSTOM_VALIDATOR = sub { if($_ =~ /\A(.*)\z/) { return (1, $1, "Passed"); } else { return (0, undef, "$_ is not valid"); } }; # Database fields description my @DB_FIELDS = ( , , , , , ); # Paging options. All are automatically verfied as an INTEGER my $RESULTS_PER_PAGE = param('RESULTS_PER_PAGE') || 0; my $MAX_RESULTS = 0; # Infinate my $PAGE_NUMBER = param('PAGE') || 0; # Search options my %SEARCH = ( num1 => , email => , ); # Other fields. These are passed to your script when pagination occurs, # but are not involved in the search my %OTHER = ( param1 => param('param1') || 0, param2 => param('param2') || 0, ); my $search = CGI::Search->new( script_name => $ENV{SCRIPT_NAME}, template => $TMPL_FILE, db_file => $DB_FILE, db_seperator => $DB_SEPERATOR, db_fields => \@DB_FIELDS, results_per_page => $RESULTS_PER_PAGE, max_results => $MAX_RESULTS, page_number => $PAGE_NUMBER, search_fields => \%SEARCH, other => \%OTHER, ); # List context -- return array-of-hashes my @data = $search->result(1) or die "Error: " . $search->errstr(); # Scalar context -- return HTML::Template object my $tmpl = $search->result(1) or die "Error: " . $search->errstr(); my %new_search = ( num1 => , email => , ); # Run a search with different parameters my $new_tmpl = $search->result(1, \%new_search); Requirements: · Perl


CGI::Search Related Software