CGI::Application(2)

戻る

#!C:/perl/bin/perl -w use Viewdata; my $webapp = Viewdata->new(); $webapp->cfg_file('config.cfg'); $webapp->header_add(-charset => 'UTF-8'); $webapp->run(); ======================================================================================= package Viewdata; use strict; use base 'CGI::Application'; use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); use CGI::Application::Plugin::ConfigAuto (qw(cfg cfg_file)); sub setup { my $self = shift; $self->start_mode('mode1'); $self->error_mode('error'); $self->mode_param('rm'); $self->run_modes( 'mode1' => 'showform', 'mode2' => 'showlist', 'mode3' => 'showdetail' ); } sub cgiapp_prerun { my $self = shift; # テンプレートファイルのあるパス $self->tmpl_path($self->cfg('template_dir')); # Connect to DBI database, with the same args as DBI->connect(); $self->dbh_config("DBI:mysql:host=localhost;database=cookbook","cbuser","cbpass"); # 漢字を表示するため $self->dbh->do("SET NAMES utf8"); } sub error { my $self = shift; my $error = shift; # $errorは"ファイルがオープンできません"みたいな、Perl(OS)が返すシステム # エラーメッセージが入ってる # ユーザに通知したくない場合は、省くといい return "システムエラーが起きました: $error"; } sub teardown { my $self = shift; # Disconnect when we're done, (Although DBI usually does this automatically) $self->dbh->disconnect(); } sub showform { my $self = shift; # Get CGI query object my $q = $self->query(); my $template = $self->load_tmpl('cook0.htm') || die "error loading tmpl"; my @loop = (); my $sth = $self->dbh->prepare("SELECT things FROM limbs"); $sth->execute; while ( my @arr = $sth->fetchrow_array ) { my %row = ( things => $arr[0], ); push(@loop, \%row); } $sth->finish; $template->param(THIS_LOOP => \@loop); return $template->output; } sub showlist { my $self = shift; # Get CGI query object my $q = $self->query(); my $template = $self->load_tmpl('cook1.htm') || die "error loading tmpl"; my @loop = (); my $sth = $self->dbh->prepare("SELECT things,legs,arms FROM limbs"); $sth->execute; while ( my @arr = $sth->fetchrow_array ) { my %row = ( things => $arr[0], legs => $arr[1], arms => $arr[2] ); push(@loop, \%row); } $sth->finish; $template->param(THIS_LOOP => \@loop); return $template->output; } sub showdetail { my $self = shift; # Get CGI query object my $q = $self->query(); my $thing = $q->param("select1"); my $template = $self->load_tmpl('cook2.htm') || die "error loading tmpl"; my @loop = (); my $sth = $self->dbh->prepare("SELECT things,legs,arms FROM limbs"); $sth->execute; while ( my @arr = $sth->fetchrow_array ) { my %row = ( things => $arr[0], legs => $arr[1], arms => $arr[2] ); if ( $thing eq $arr[0] ) { push(@loop, \%row); } } $sth->finish; $template->param(THIS_LOOP => \@loop); return $template->output; } 1; # Perl requires this at the end of all modules ======================================================================================= cook0.htm <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>選択画面</TITLE> </HEAD> <BODY bgcolor="#FFFFFF"> <h2>選択画面</h2> <hr color="#003366"> <form action="" method="post"> <select name="select1" size=1> <TMPL_LOOP NAME="this_loop"> <option value="<TMPL_VAR NAME="things">"><TMPL_VAR NAME="things"> </TMPL_LOOP> </select><p> <input type="submit" value="選択">押してください<p> <input type="hidden" name="rm" value="mode3"> </form> <hr color="#003366"> <form action="" method="post"> <input type="submit" value="テーブル表示">押してください<p> <input type="hidden" name="rm" value="mode2"> </form> </BODY> </HTML> cook1.htm <HTML> <HEAD> <TITLE>テーブル表示</TITLE> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> </HEAD> <BODY bgcolor="#FFFFFF"> <center> <h2>テーブル表示</h2> <hr color="#003366"> <table width=75% align="center" border=1 cellpadding=5> <tr bgcolor="#3399FF" align="center"><th width=60%>Things</th><th width=20%>Legs</th><th width=20%>Arms</th></tr> <TMPL_LOOP NAME="this_loop"> <tr bgcolor="lightcyan" align="left"><td><TMPL_VAR NAME="things"></td><td><TMPL_VAR NAME="legs"></td><td><TMPL_VAR NAME="arms"></td></tr> </TMPL_LOOP> </table> <hr color="#003366"> <a href="http://localhost/cgi-bin/data.cgi">return</a> </BODY> </HTML> cook2.htm <HTML> <HEAD> <TITLE>一項目を表示</TITLE> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> </HEAD> <BODY bgcolor="#FFFFFF"> <center> <h2>一項目を表示</h2> <hr color="#003366"> <table width=75% align="center" border=1 cellpadding=5> <tr bgcolor="#3399FF" align="center"><th width=60%>Things</th><th width=20%>Legs</th><th width=20%>Arms</th></tr> <TMPL_LOOP NAME="this_loop"> <tr bgcolor="lightcyan" align="left"><td><TMPL_VAR NAME="things"></td><td><TMPL_VAR NAME="legs"></td><td><TMPL_VAR NAME="arms"></td></tr> </TMPL_LOOP> </table> <hr color="#003366"> <a href="http://localhost/cgi-bin/data.cgi">戻る</a> </BODY> </HTML> ======================================================================================= ppm> install CGI-Application-Plugin-ConfigAuto ==================== Install 'Config-IniFiles' version 2.38 in ActivePerl 5.8.4.810. ==================== Downloaded 32437 bytes. Extracting 5/5: blib/arch/auto/Config/IniFiles/.exists Installing C:\Perl\html\site\lib\Config\IniFiles.html Installing C:\Perl\site\lib\Config\IniFiles.pm Successfully installed Config-IniFiles version 2.38 in ActivePerl 5.8.4.810. ==================== Install 'Config-Auto' version 0.12 in ActivePerl 5.8.4.810. ==================== Downloaded 6217 bytes. Extracting 5/5: blib/arch/auto/Config/Auto/.exists Installing C:\Perl\html\site\lib\Config\Auto.html Installing C:\Perl\site\lib\Config\Auto.pm Successfully installed Config-Auto version 0.12 in ActivePerl 5.8.4.810. ==================== Install 'CGI-Application-Plugin-ConfigAuto' version 1.10 in ActivePerl 5.8.4.810 . ==================== Downloaded 3766 bytes. Extracting 5/5: blib/arch/auto/CGI/Application/Plugin/ConfigAuto/.exists Installing C:\Perl\html\site\lib\CGI\Application\Plugin\ConfigAuto.html Installing C:\Perl\site\lib\CGI\Application\Plugin\ConfigAuto.pm Successfully installed CGI-Application-Plugin-ConfigAuto version 1.10 in ActiveP erl 5.8.4.810. ppm> quit c:\database\perl>ppm PPM - Programmer's Package Manager version 3.1. Copyright (c) 2001 ActiveState Corp. All Rights Reserved. ActiveState is a devision of Sophos. Entering interactive shell. Using Term::ReadLine::Stub as readline library. Type 'help' to get started. ppm> install CGI-Application-Plugin-ValidateRM ==================== Install 'Carp-Clan' version 5.8 in ActivePerl 5.8.4.810. ==================== Downloaded 5551 bytes. Extracting 11/11: blib/arch/auto/Carp/Clan/.exists Installing C:\Perl\html\site\lib\Carp\Clan.html Installing C:\Perl\site\lib\Carp\Clan.pm Installing C:\Perl\site\lib\Carp\Clan.pod Successfully installed Carp-Clan version 5.8 in ActivePerl 5.8.4.810. ==================== Install 'Bit-Vector' version 6.4 in ActivePerl 5.8.4.810. ==================== Downloaded 124862 bytes. Extracting 16/16: blib/arch/auto/Bit/Vector/Vector.lib Installing C:\Perl\site\lib\auto\Bit\Vector\Vector.bs Installing C:\Perl\site\lib\auto\Bit\Vector\Vector.dll Installing C:\Perl\site\lib\auto\Bit\Vector\Vector.exp Installing C:\Perl\site\lib\auto\Bit\Vector\Vector.lib Installing C:\Perl\html\site\lib\Bit\Vector.html Installing C:\Perl\html\site\lib\Bit\Vector\Overload.html Installing C:\Perl\html\site\lib\Bit\Vector\String.html Files found in blib\arch: installing files in blib\lib into architecture depende nt library tree Installing C:\Perl\site\lib\Bit\Vector.pm Installing C:\Perl\site\lib\Bit\Vector.pod Installing C:\Perl\site\lib\Bit\Vector\Overload.pm Installing C:\Perl\site\lib\Bit\Vector\Overload.pod Installing C:\Perl\site\lib\Bit\Vector\String.pm Installing C:\Perl\site\lib\Bit\Vector\String.pod Successfully installed Bit-Vector version 6.4 in ActivePerl 5.8.4.810. ==================== Install 'Carp-Clan' version 5.8 in ActivePerl 5.8.4.810. ==================== Downloaded 5551 bytes. Extracting 11/11: blib/arch/auto/Carp/Clan/.exists Successfully installed Carp-Clan version 5.8 in ActivePerl 5.8.4.810. ==================== Install 'Date-Calc' version 5.4 in ActivePerl 5.8.4.810. ==================== Downloaded 184821 bytes. Extracting 22/22: blib/arch/auto/Date/Calc/Calc.lib Installing C:\Perl\site\lib\auto\Date\Calc\Calc.bs Installing C:\Perl\site\lib\auto\Date\Calc\Calc.dll Installing C:\Perl\site\lib\auto\Date\Calc\Calc.exp Installing C:\Perl\site\lib\auto\Date\Calc\Calc.lib Installing C:\Perl\html\site\lib\Date\Calc.html Installing C:\Perl\html\site\lib\Date\Calendar.html Installing C:\Perl\html\site\lib\Date\Calc\Object.html Installing C:\Perl\html\site\lib\Date\Calendar\Profiles.html Installing C:\Perl\html\site\lib\Date\Calendar\Year.html Files found in blib\arch: installing files in blib\lib into architecture depende nt library tree Installing C:\Perl\site\lib\Date\Calc.pm Installing C:\Perl\site\lib\Date\Calc.pod Installing C:\Perl\site\lib\Date\Calendar.pm Installing C:\Perl\site\lib\Date\Calendar.pod Installing C:\Perl\site\lib\Date\Calc\Object.pm Installing C:\Perl\site\lib\Date\Calc\Object.pod Installing C:\Perl\site\lib\Date\Calendar\Profiles.pm Installing C:\Perl\site\lib\Date\Calendar\Profiles.pod Installing C:\Perl\site\lib\Date\Calendar\Year.pm Installing C:\Perl\site\lib\Date\Calendar\Year.pod Successfully installed Date-Calc version 5.4 in ActivePerl 5.8.4.810. ==================== Install 'File-MMagic' version 1.27 in ActivePerl 5.8.4.810. ==================== Downloaded 19310 bytes. Extracting 5/5: blib/arch/auto/File/MMagic/.exists Installing C:\Perl\html\site\lib\File\MMagic.html Installing C:\Perl\site\lib\File\MMagic.pm Successfully installed File-MMagic version 1.27 in ActivePerl 5.8.4.810. ==================== Install 'Image-Size' version 3.01 in ActivePerl 5.8.4.810. ==================== Downloaded 23254 bytes. Extracting 27/27: blib/arch/auto/Image/Size/.exists Installing C:\Perl\html\bin\imgsize.html Installing C:\Perl\html\site\lib\Image\Size.html Installing C:\Perl\site\lib\auto\Image\Size\autosplit.ix Installing C:\Perl\site\lib\auto\Image\Size\bmpsize.al Installing C:\Perl\site\lib\auto\Image\Size\gifsize.al Installing C:\Perl\site\lib\auto\Image\Size\jpegsize.al Installing C:\Perl\site\lib\auto\Image\Size\mngsize.al Installing C:\Perl\site\lib\auto\Image\Size\pcdsize.al Installing C:\Perl\site\lib\auto\Image\Size\pngsize.al Installing C:\Perl\site\lib\auto\Image\Size\ppmsize.al Installing C:\Perl\site\lib\auto\Image\Size\psdsize.al Installing C:\Perl\site\lib\auto\Image\Size\swfmxsize.al Installing C:\Perl\site\lib\auto\Image\Size\swfsize.al Installing C:\Perl\site\lib\auto\Image\Size\tiffsize.al Installing C:\Perl\site\lib\auto\Image\Size\xbmsize.al Installing C:\Perl\site\lib\auto\Image\Size\xpmsize.al Installing C:\Perl\site\lib\Image\Size.pm Installing C:\Perl\bin\imgsize Installing C:\Perl\bin\imgsize.bat Successfully installed Image-Size version 3.01 in ActivePerl 5.8.4.810. ==================== Install 'MIME-Types' version 1.19 in ActivePerl 5.8.4.810. ==================== Downloaded 16247 bytes. Extracting 14/14: blib/arch/auto/MIME/Types/.exists Installing C:\Perl\html\site\lib\MIME\Type.html Installing C:\Perl\html\site\lib\MIME\Types.html Installing C:\Perl\site\lib\MIME\Type.pm Installing C:\Perl\site\lib\MIME\Type.pod Installing C:\Perl\site\lib\MIME\Types.pm Installing C:\Perl\site\lib\MIME\Types.pod Successfully installed MIME-Types version 1.19 in ActivePerl 5.8.4.810. ==================== Install 'Regexp-Common' version 2.120 in ActivePerl 5.8.4.810. ==================== Downloaded 92999 bytes. Extracting 69/69: blib/arch/auto/Regexp/Common/.exists Installing C:\Perl\html\site\lib\Regexp\Common.html Installing C:\Perl\html\site\lib\Regexp\Common\balanced.html Installing C:\Perl\html\site\lib\Regexp\Common\CC.html Installing C:\Perl\html\site\lib\Regexp\Common\comment.html Installing C:\Perl\html\site\lib\Regexp\Common\delimited.html Installing C:\Perl\html\site\lib\Regexp\Common\lingua.html Installing C:\Perl\html\site\lib\Regexp\Common\list.html Installing C:\Perl\html\site\lib\Regexp\Common\net.html Installing C:\Perl\html\site\lib\Regexp\Common\number.html Installing C:\Perl\html\site\lib\Regexp\Common\profanity.html Installing C:\Perl\html\site\lib\Regexp\Common\SEN.html Installing C:\Perl\html\site\lib\Regexp\Common\URI.html Installing C:\Perl\html\site\lib\Regexp\Common\whitespace.html Installing C:\Perl\html\site\lib\Regexp\Common\zip.html Installing C:\Perl\html\site\lib\Regexp\Common\_support.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\fax.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\file.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\ftp.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\gopher.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\http.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\news.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\pop.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\prospero.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\RFC1035.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\RFC1738.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\RFC1808.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\RFC2384.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\RFC2396.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\RFC2806.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\tel.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\telnet.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\tv.html Installing C:\Perl\html\site\lib\Regexp\Common\URI\wais.html Installing C:\Perl\site\lib\Regexp\Common.pm Installing C:\Perl\site\lib\Regexp\Common\balanced.pm Installing C:\Perl\site\lib\Regexp\Common\CC.pm Installing C:\Perl\site\lib\Regexp\Common\comment.pm Installing C:\Perl\site\lib\Regexp\Common\delimited.pm Installing C:\Perl\site\lib\Regexp\Common\lingua.pm Installing C:\Perl\site\lib\Regexp\Common\list.pm Installing C:\Perl\site\lib\Regexp\Common\net.pm Installing C:\Perl\site\lib\Regexp\Common\number.pm Installing C:\Perl\site\lib\Regexp\Common\profanity.pm Installing C:\Perl\site\lib\Regexp\Common\SEN.pm Installing C:\Perl\site\lib\Regexp\Common\URI.pm Installing C:\Perl\site\lib\Regexp\Common\whitespace.pm Installing C:\Perl\site\lib\Regexp\Common\zip.pm Installing C:\Perl\site\lib\Regexp\Common\_support.pm Installing C:\Perl\site\lib\Regexp\Common\URI\fax.pm Installing C:\Perl\site\lib\Regexp\Common\URI\file.pm Installing C:\Perl\site\lib\Regexp\Common\URI\ftp.pm Installing C:\Perl\site\lib\Regexp\Common\URI\gopher.pm Installing C:\Perl\site\lib\Regexp\Common\URI\http.pm Installing C:\Perl\site\lib\Regexp\Common\URI\news.pm Installing C:\Perl\site\lib\Regexp\Common\URI\pop.pm Installing C:\Perl\site\lib\Regexp\Common\URI\prospero.pm Installing C:\Perl\site\lib\Regexp\Common\URI\RFC1035.pm Installing C:\Perl\site\lib\Regexp\Common\URI\RFC1738.pm Installing C:\Perl\site\lib\Regexp\Common\URI\RFC1808.pm Installing C:\Perl\site\lib\Regexp\Common\URI\RFC2384.pm Installing C:\Perl\site\lib\Regexp\Common\URI\RFC2396.pm Installing C:\Perl\site\lib\Regexp\Common\URI\RFC2806.pm Installing C:\Perl\site\lib\Regexp\Common\URI\tel.pm Installing C:\Perl\site\lib\Regexp\Common\URI\telnet.pm Installing C:\Perl\site\lib\Regexp\Common\URI\tv.pm Installing C:\Perl\site\lib\Regexp\Common\URI\wais.pm Successfully installed Regexp-Common version 2.120 in ActivePerl 5.8.4.810. ==================== Install 'Data-FormValidator' version 4.50 in ActivePerl 5.8.4.810. ==================== Downloaded 64481 bytes. Extracting 22/22: blib/arch/auto/Data/FormValidator/.exists l Installing C:\Perl\html\site\lib\Data\FormValidator.html Installing C:\Perl\html\site\lib\Data\FormValidator\Constraints.html Installing C:\Perl\html\site\lib\Data\FormValidator\ConstraintsFactory.html Installing C:\Perl\html\site\lib\Data\FormValidator\Filters.html Installing C:\Perl\html\site\lib\Data\FormValidator\Results.html Installing C:\Perl\html\site\lib\Data\FormValidator\Constraints\Dates.html Installing C:\Perl\html\site\lib\Data\FormValidator\Constraints\Upload.html Installing C:\Perl\site\lib\Data\FormValidator.pm Installing C:\Perl\site\lib\Data\FormValidator\Constraints.pm Installing C:\Perl\site\lib\Data\FormValidator\ConstraintsFactory.pm Installing C:\Perl\site\lib\Data\FormValidator\Filters.pm Installing C:\Perl\site\lib\Data\FormValidator\Results.pm Installing C:\Perl\site\lib\Data\FormValidator\Constraints\Dates.pm Installing C:\Perl\site\lib\Data\FormValidator\Constraints\Upload.pm Successfully installed Data-FormValidator version 4.50 in ActivePerl 5.8.4.810. ==================== Install 'HTML-FillInForm' version 1.06 in ActivePerl 5.8.4.810. ==================== Downloaded 6966 bytes. Extracting 5/5: blib/arch/auto/HTML/FillInForm/.exists Installing C:\Perl\html\site\lib\HTML\FillInForm.html Installing C:\Perl\site\lib\HTML\FillInForm.pm Successfully installed HTML-FillInForm version 1.06 in ActivePerl 5.8.4.810. ==================== Install 'CGI-Application-Plugin-ValidateRM' version 2.1 in ActivePerl 5.8.4.810. ==================== Downloaded 6201 bytes. Extracting 6/6: blib/arch/auto/CGI/Application/Plugin/ValidateRM/.exists Installing C:\Perl\html\site\lib\CGI\Application\Plugin\ValidateRM.html Installing C:\Perl\site\lib\auto\CGI\Application\Plugin\ValidateRM\autosplit.ix Installing C:\Perl\site\lib\CGI\Application\Plugin\ValidateRM.pm Successfully installed CGI-Application-Plugin-ValidateRM version 2.1 in ActivePe rl 5.8.4.810. ppm>

戻る