home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_a540429040325eadc044efe2fcd17956
< prev
next >
Wrap
Text File
|
2000-03-23
|
14KB
|
358 lines
<HTML>
<HEAD>
<TITLE>CGI::Screen - Perl extension for easy creation of multi screen CGI scripts</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> CGI::Screen - Perl extension for easy creation of multi screen CGI scripts</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#warning">WARNING</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<UL>
<LI><A HREF="#the constructor new">The constructor <CODE>new</CODE></A></LI>
<LI><A HREF="#adding screens">Adding Screens</A></LI>
<LI><A HREF="#moving between screens">Moving between screens</A></LI>
<LI><A HREF="#the callbacks">The callbacks</A></LI>
<LI><A HREF="#authentication">Authentication</A></LI>
<LI><A HREF="#customizing the title">Customizing the Title</A></LI>
<LI><A HREF="#customizing the headline">Customizing the Headline</A></LI>
<LI><A HREF="#customizing the trailer">Customizing the Trailer</A></LI>
<LI><A HREF="#multiple forms">Multiple Forms</A></LI>
<LI><A HREF="#non html screens">Non HTML screens</A></LI>
<LI><A HREF="#keeping parameter values">Keeping parameter values</A></LI>
</UL>
<LI><A HREF="#bugs">BUGS</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
<LI><A HREF="#see also">SEE ALSO</A></LI>
<LI><A HREF="#acknowledgements">ACKNOWLEDGEMENTS</A></LI>
<LI><A HREF="#copyright">Copyright</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>CGI::Screen - Perl extension for easy creation of multi screen CGI scripts</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use CGI::Screen;
use vars qw(@ISA);
@ISA = qw(CGI::Screen);</PRE>
<PRE>
my $query = __PACKAGE__->new;</PRE>
<PRE>
$query->dispatch;</PRE>
<P>
<HR>
<H1><A NAME="warning">WARNING</A></H1>
<P>This is <STRONG>alpha</STRONG> software. User visible changes can happen any time.</P>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P><STRONG>CGI::Screen</STRONG> is a subclass of <CODE>CGI</CODE> which allows the <CODE>esay(TM)</CODE>
creation of simple multi screen CGI scripts. By 'multi screen' I mean
scripts which present different screens to the user when called with
different parameters. This is the common case for scripts linking to
themselves.</P>
<P>To use <STRONG>CGI::Screen</STRONG>, you have to subclass it. For each screen you
want to present to the user, you must create a method
<EM>screen_name</EM><CODE>screen_</CODE>. This method has to produce the HTML code for
the screen. CGI::Screen does generate HTTP headers and an HTML
framework for you. The HTML framework already contains the <CODE>FORM</CODE>
tags. You can customize the HTTP headers and the HTML framework by
providing callback methods.</P>
<P>CGI::Screen keeps track of the CGI parameters used in your screen and
passes old parameters which are not used in the current screen.</P>
<P>It highjacks the parameters <CODE>screen_</CODE>* to dispatch the different
screens the script implements. The <CODE>screen_user</CODE> and <CODE>screen_passwd</CODE>
fields are used if you enable the builtin simple authentication. In
general you should advice your HTTP server to do authentication. But
sometimes it is convenient to check the authentication at the script
level. Especially if you do not have access to your server's
configuration.</P>
<P>
<H2><A NAME="the constructor new">The constructor <CODE>new</CODE></A></H2>
<P>If the first parameter of <CODE>new</CODE> is the string <CODE>-screen</CODE> the second
argument must be a hash reference specifying the options for the
subclass. Other parameters are passed to the constructor of <CODE>CGI</CODE>.</P>
<DL>
<DT><STRONG><A NAME="item_%2Ddont_cut_loops"><CODE>-dont_cut_loops</CODE></A></STRONG><BR>
<DD>
Normaly the history of pages will not be extended if the current page is
the same as the last page. So looping on a page will not change the
result of the <CODE>last_screen</CODE> method. If the option <A HREF="#item_%2Ddont_cut_loops"><CODE>-dont_cut_loops</CODE></A>
is provided and true, the page will recorded twice. A third visit will
be interpreted as jump back to the first visit.
<P><EM>That sounds weird. Will have to figure out a way to recognize back
jumps independent of the history.</EM></P>
<P></P></DL>
<P>
<H2><A NAME="adding screens">Adding Screens</A></H2>
<P>All applications should provide a <STRONG>main</STRONG> screen by defining a method
<CODE>main_screen</CODE>. This method is called if no (existing) screen is
specified in the parameters. The method is called with three
arguments: The query object, the screen name and the screen title
(More precisely the third parameter, if present, is the text on the
button or anchor which caused the jump to this page).</P>
<P>So the minimal application looks like this:</P>
<PRE>
use CGI::Screen;
use vars qw(@ISA);
@ISA = qw(CGI::Screen);
</PRE>
<PRE>
my $query = __PACKAGE__->new;</PRE>
<PRE>
$query->dispatch;</PRE>
<PRE>
sub main_screen {
my $query = shift;</PRE>
<PRE>
print $query->p('This is the Main Screen');
}</PRE>
<P>That is not too exciting. Let us add a second screen and allow
navigation between the screens:</P>
<PRE>
sub main_screen {
my $query = shift;
</PRE>
<PRE>
print
$query->p('This is the Main Screen'),
$query->goto_screen('second', 'Another Screen');
}
sub second_screen {
my $query = shift;</PRE>
<PRE>
print
$query->p('This is the Other Screen'),
$query->goto_screen('main', 'Back to Main Screen');</PRE>
<PRE>
}</PRE>
<P>
<H2><A NAME="moving between screens">Moving between screens</A></H2>
<P>Use the method <CODE>goto_screen</CODE> to produce a button for switching to
another screen. You can also produce an anchor instead of a button by
calling <CODE>link_to_screen</CODE> instead of <CODE>goto_screen</CODE>. You may pass
additional parameters to encode:</P>
<PRE>
for my $docid (keys %score) {
print $query->link_to_screen('display', $title{$docid},
'docid' => $docid,
'score' => $score{$docid});
}</PRE>
<P>For convenience, CGI::Screen keeps track of the last screen for you so
that you can link to the previous page. Note that only the last seven
screens are saved:</P>
<PRE>
my $screen = $query->last_screen;
print
$query->p("You came from screen $screen. Press "),
$query->goto_screen($query->last_screen),
$query->p(" to go back");</PRE>
<P><CODE>last_screen</CODE> returns screen name and title in list context and
screen name in scalar context. Do not use the CGI parameters
<CODE>screen_last_*</CODE> since they are changed before you can get hold of
them <CODE>;-P</CODE></P>
<P>
<H2><A NAME="the callbacks">The callbacks</A></H2>
<P>All callbacks are called with three arguments: The query object, the
screen name and the screen title (= button/anchor text). Callbacks
should return a string.</P>
<DL>
<DT><STRONG><A NAME="item_application"><CODE>application</CODE></A></STRONG><BR>
<DD>
The <A HREF="#item_application"><CODE>application</CODE></A> method returns a string which is used
in the default <A HREF="#item_title"><CODE>title</CODE></A> and <A HREF="#item_headline"><CODE>headline</CODE></A> callbacks. The Default method
returns the string <CODE>"CGI::Screen Test"</CODE> and should definitely be
overwritten by your application.
<P></P>
<DT><STRONG><A NAME="item_title"><CODE>title</CODE></A></STRONG><BR>
<DD>
The result of the method is used in the HTTP header and in the default
headline. It defaults to the <EM>application</EM>.
<P></P>
<DT><STRONG><A NAME="item_headline"><CODE>headline</CODE></A></STRONG><BR>
<DD>
The <A HREF="#item_headline"><CODE>headline</CODE></A> method should return a chunk of HTML code to start the
Screen. It defaults to the <EM>title</EM> enclosed in <CODE>H1</CODE> tags.
<P></P></DL>
<P>
<H2><A NAME="authentication">Authentication</A></H2>
<P>To enable password authentication, define a method
<CODE>check_auth_user</CODE>. The dispatcher will call the method with the user
and password entered by the user. The method should return true if the
authentication succeeded and false otherwise. The dispatcher will
present the <CODE>login_screen</CODE> if the authentication failed.</P>
<PRE>
sub check_auth_user {
my ($query, $user, $passwd) = @_;</PRE>
<PRE>
$user eq 'pfeifer';
}</PRE>
<P>For IP address based authentication define the method
<CODE>check_auth_ip</CODE>.</P>
<PRE>
sub check_auth_ip {
my ($query, $ipaddr) = @_;</PRE>
<PRE>
$ipaddr =~ /^(193\.96\.65\.|139\.4\.36\.)/;
}</PRE>
<P>If you do not like the default login screen, overwrite with your own
<CODE>login_screen</CODE>. Use the CGI parameters <CODE>screen_user</CODE> and
<CODE>screen_passwd</CODE>.</P>
<P>
<H2><A NAME="customizing the title">Customizing the Title</A></H2>
<P>You may provide a custom <A HREF="#item_title"><CODE>title</CODE></A> method to generate a title for your
screens.</P>
<PRE>
sub title {
my ($query, $screen) = shift;
</PRE>
<PRE>
$query->application . ': ' . $screen;
}</PRE>
<P>
<H2><A NAME="customizing the headline">Customizing the Headline</A></H2>
<P>You may provide a custom <A HREF="#item_headline"><CODE>headline</CODE></A> method to generate a HTML chunk
to start your screens.</P>
<PRE>
sub headline { $_[0]->h1(title(@_)) }</PRE>
<P>You should overwrite the <A HREF="#item_application"><CODE>application</CODE></A> method if you use the default
title and headline.</P>
<PRE>
sub application { 'CGI::Screen Test' }</PRE>
<P>
<H2><A NAME="customizing the trailer">Customizing the Trailer</A></H2>
<P>For a custom Trailer, define the <CODE>trailer</CODE> method.</P>
<PRE>
sub trailer {
my ($query, $screen) = shift;
</PRE>
<PRE>
"End of Screen $screen";
}</PRE>
<P>
<H2><A NAME="multiple forms">Multiple Forms</A></H2>
<P>If you want to have multiple forms on one screen, call the method
<CODE>new_form</CODE>.</P>
<PRE>
sub multi_screen {
my $query = shift;</PRE>
<PRE>
print
$query->p('This is the Main Screen'),
$query->textfield('foo'),
$query->goto('First'),
$query->new_form,
$query->textfield('foo'),
$query->goto('Second');
}</PRE>
<P>
<H2><A NAME="non html screens">Non HTML screens</A></H2>
<P>You can create non HTML screens by defining a <EM>name</EM><CODE>_data</CODE> method
instead of a <name><CODE>_screen</CODE> method. For <CODE>data</CODE> screens you have
to generate HTTP headers yourself.</P>
<PRE>
sub gif_data {
my $query = shift;
</PRE>
<PRE>
print $query->header(
-type => 'image/gif',
-status => '200 OK',
-expires => '+120s',
);
my $font = $query->param('font');
my $w = GD::Font->$font()->width;
my $h = GD::Font->$font()->height;
my $im = GD::Image->new((length($query->param('foo'))+2)*$w,$h);
my $white = $im->colorAllocate(255,255,255);
my $red = $im->colorAllocate(255,0,0);
my $black = $im->colorAllocate(0,0,0);
$im->transparent($white);
$im->arc(8,8,5,5,0,360,$red);
$im->string(GD::Font->$font(),10,0,$query->param('foo'),$black);
print $im->gif;
}</PRE>
<P>
<H2><A NAME="keeping parameter values">Keeping parameter values</A></H2>
<P>CGI::Screen keeps track of the CGI parameters used in the current
form. It simply looks at the first parameter in any call to a CGI
method. If the first parameter is <CODE>-name</CODE>, the second parameter is
marked as <EM>used parameter</EM>. CGI::Screen passed all current parameter
values not used in hidden fields or in the query string of an
anchor. So do not use old style CGI calls to bypass this mechanism or
you will end up with multiple values for the parameters.</P>
<P>If you want to get rid of a parameter, you must explicitly call the
<A HREF="../../../lib/Pod/perlfunc.html#item_delete"><CODE>delete</CODE></A> method of CGI.</P>
<P>
<HR>
<H1><A NAME="bugs">BUGS</A></H1>
<P>Support for importing from CGI.pm is incomplete.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Ulrich Pfeifer <<EM><A HREF="mailto:pfeifer@wait.de">pfeifer@wait.de</A></EM>></P>
<P>
<HR>
<H1><A NAME="see also">SEE ALSO</A></H1>
<P>The <CODE>CGI(3)</CODE> manual and the demo CGI script <EM>eg/screen</EM> included in the
distribution.</P>
<P>
<HR>
<H1><A NAME="acknowledgements">ACKNOWLEDGEMENTS</A></H1>
<P>I wish to thank Andreas Koenig <EM><A HREF="mailto:koenig@kulturbox.de">koenig@kulturbox.de</A></EM> for the
fruitful discussion about the design of this module.</P>
<P>
<HR>
<H1><A NAME="copyright">Copyright</A></H1>
<P>The <STRONG>CGI::Screen</STRONG> module is Copyright (c) 1997,1998 Ulrich
Pfeifer. Germany. All rights reserved.</P>
<P>You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> CGI::Screen - Perl extension for easy creation of multi screen CGI scripts</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>