package Querylet::CGI; use CGI qw/param/; use base(Querylet::Input); use warnings; use strict; =head1 NAME Querylet::CGI - turn a querylet into a web application =head1 VERSION version 0.142 $Id: /my/cs/projects/q/cgi/trunk/lib/Querylet/CGI.pm 28168 2006-11-16T13:28:06.203711Z rjbs $ =cut our $VERSION = '0.142'; =head1 SYNOPSIS use Querylet; use Querylet::CGI; query: SELECT firstname, age FROM people WHERE lastname = ? ORDER BY firstname input type: cgi input: lastname query parameter: $input->{lastname} =head1 DESCRIPTION Querylet::CGI provides an input handler for Querylet, retrieving input parameters from the CGI environment. If not all required inputs are found, it changes the Query object's output type, causing it to produce a form requesting the required input parameters. =head1 METHODS =over 4 =item C<< default_type >> Querylet::CGI acts as a Querylet::Input module, and registers itself as an input handler when used. The default type to register is 'cgi' =cut sub default_type { 'cgi' } =item C<< handler >> The default registered handler will retrieve parameters from the CGI environment using the CGI module. =cut sub handler { \&_from_cgi } sub _from_cgi { my $q = shift; my $parameter = shift; if (defined param($parameter)) { $q->{input}->{$parameter} = param($parameter); $q->output_type('cgi_html_table') unless ($q->output_type eq 'cgi_html_form'); } else { $q->{input}->{$parameter} = undef; $q->output_type('cgi_html_form'); } } Querylet::Query->register_output_handler(cgi_html_form => \&_as_form); sub _as_form { my $q = shift; my $form = "Content-type: text/html\n\n"; $form .= "querylet input required"; $form .= "
"; $form .= "" for keys %{$q->{input}}; $form .= "
$_
"; $form .= "
\n"; return $form; } Querylet::Query->register_output_handler(cgi_html_table => \&_as_html); sub _as_html { my $q = shift; my $results = $q->results; my $columns = $q->columns; my $html = "Content-type: text/html\n\n"; $html .= "results of query"; $html .= ""; $html .= join('', map { "" } @$columns); $html .= "\n"; if (@$results) { $html .= "" . join('', map { "" } @$_{@$columns}). "\n" foreach (@$results); } else { $html .= "\n"; } $html .= "
" . $q->header($_) . "
$_
no results
\n"; } =back =head1 AUTHOR Ricardo SIGNES, C<< >> =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 COPYRIGHT Copyright 2004 Ricardo SIGNES, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1;