⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgiprogramming.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
</FONT>$page =3D param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>".State"</FONT><FONT color=3D#00ffff>)</FONT> || <FONT =
color=3D#00cd00>"Default"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$page <FONT color=3D#98fb98>eq</FONT> <FONT =
color=3D#00cd00>"Default"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    front_page<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>elsif</FONT> <FONT =
color=3D#00ffff>(</FONT>$page <FONT color=3D#98fb98>eq</FONT> <FONT =
color=3D#00cd00>"Checkout"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    checkout<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>no</FONT>_such_page<FONT =
color=3D#00ffff>();</FONT>         <FONT color=3D#bebebe># when we get a =
.State that doesn't exist
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>%States</FONT> =3D <FONT =
color=3D#00ffff>(</FONT>
    <FONT color=3D#00cd00>'Default'</FONT>     =3D&gt; \&amp;front_page,
    <FONT color=3D#00cd00>'Shirt'</FONT>       =3D&gt; \&amp;shirt,
    <FONT color=3D#00cd00>'Sweater'</FONT>     =3D&gt; \&amp;sweater,
    <FONT color=3D#00cd00>'Checkout'</FONT>    =3D&gt; \&amp;checkout,
    <FONT color=3D#00cd00>'Card'</FONT>        =3D&gt; =
\&amp;credit_card,
    <FONT color=3D#00cd00>'Order'</FONT>       =3D&gt; \&amp;order,
    <FONT color=3D#00cd00>'Cancel'</FONT>      =3D&gt; \&amp;front_page,
<FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>$States</FONT><FONT color=3D#00ffff>{</FONT>$page<FONT =
color=3D#00ffff>})</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#cdcd00>$States</FONT><FONT =
color=3D#00ffff>{</FONT>$page<FONT color=3D#00ffff>}</FONT>-&gt;<FONT =
color=3D#00ffff>();</FONT>   <FONT color=3D#bebebe># call the correct =
subroutine</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>no</FONT>_such_page<FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>((</FONT>$state, $sub<FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#ff7f50>each</FONT> <FONT =
color=3D#cdcd00>%States</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    $sub-&gt;<FONT color=3D#00ffff>(</FONT> $page <FONT =
color=3D#98fb98>eq</FONT> $state <FONT color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>t_shirt</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$active</FONT> =
=3D <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>

    <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT>$active<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT> hidden<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"size"</FONT><FONT =
color=3D#00ffff>)</FONT>, hidden<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"color"</FONT><FONT color=3D#00ffff>);</FONT>
        <FONT color=3D#ffa500>return</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>

    <FONT color=3D#ff7f50>print</FONT> p<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"You want to buy a =
t-shirt?"</FONT><FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ff7f50>print</FONT> p<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Size: "</FONT>, =
popup_menu<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>'size'</FONT>, <FONT color=3D#00ffff>[</FONT> <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>XL L M S XS<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>]));</FONT>
    <FONT color=3D#ff7f50>print</FONT> p<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Color:"</FONT>, =
popup_menu<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>'color'</FONT>, <FONT color=3D#00ffff>[</FONT> <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>Black White<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>]));</FONT>

    <FONT color=3D#ff7f50>print</FONT> p<FONT color=3D#00ffff>(</FONT> =
to_page<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Shoes"</FONT><FONT color=3D#00ffff>)</FONT>, =
to_page<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Checkout"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT> header<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Program =
Title"</FONT><FONT color=3D#00ffff>)</FONT>, start_html<FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#ff7f50>print</FONT> standard_header<FONT =
color=3D#00ffff>()</FONT>, begin_form<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>((</FONT>$state, $sub<FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#ff7f50>each</FONT> <FONT =
color=3D#cdcd00>%States</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    $sub-&gt;<FONT color=3D#00ffff>(</FONT> $page <FONT =
color=3D#98fb98>eq</FONT> $state <FONT color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> standard_footer<FONT =
color=3D#00ffff>()</FONT>, end_form<FONT color=3D#00ffff>()</FONT>, =
end_html<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1046>Saving a Form to a File or Mail =
Pipe</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# first open and exclusively lock the file
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FH, <FONT =
color=3D#00cd00>"&gt;&gt;/tmp/formlog"</FONT><FONT =
color=3D#00ffff>)</FONT>              <FONT color=3D#98fb98>or</FONT> =
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't append to =
formlog: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>flock</FONT><FONT color=3D#00ffff>(</FONT>FH, =
<FONT color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>)</FONT>             =
               <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't flock formlog: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe># either using the procedural interface
</FONT><FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>CGI</FONT> =
<FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>:standard<FONT color=3D#00ffff>);</FONT>
save_parameters<FONT color=3D#00ffff>(</FONT>*FH<FONT =
color=3D#00ffff>);</FONT>                   <FONT color=3D#bebebe># with =
CGI::save
</FONT>
<FONT color=3D#bebebe># or using the object interface
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>CGI</FONT><FONT color=3D#00ffff>;</FONT>
$query =3D CGI-&gt;new<FONT color=3D#00ffff>();</FONT>
$query-&gt;save<FONT color=3D#00ffff>(</FONT>*FH<FONT =
color=3D#00ffff>);</FONT>

<FONT color=3D#98fb98>close</FONT><FONT color=3D#00ffff>(</FONT>FH<FONT =
color=3D#00ffff>)</FONT>                               <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't close formlog: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>CGI</FONT> =
<FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>:standard<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>MAIL, =
<FONT color=3D#00cd00>"|/usr/lib/sendmail -oi -t"</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't fork sendmail: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> MAIL &lt;&lt;EOF<FONT =
color=3D#00ffff>;</FONT>
From: $<FONT color=3D#cdcd00>0</FONT> <FONT color=3D#00ffff>(</FONT>your =
cgi script<FONT color=3D#00ffff>)</FONT>
To: hisname\<FONT color=3D#cdad00>@hishost</FONT>.com
Subject: mailed form submission

EOF
save_parameters<FONT color=3D#00ffff>(</FONT>*MAIL<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>MAIL<FONT color=3D#00ffff>)</FONT>               =
              <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't close sendmail: =
$!"</FONT><FONT color=3D#00ffff>;</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"_timestamp"</FONT>, <FONT color=3D#ff7f50>scalar</FONT> =
<FONT color=3D#98fb98>localtime</FONT><FONT color=3D#00ffff>);</FONT>
param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"_environs"</FONT>, <FONT =
color=3D#cdcd00>%ENV</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>CGI</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>FORMS, =
<FONT color=3D#00cd00>"&lt; /tmp/formlog"</FONT><FONT =
color=3D#00ffff>)</FONT>       <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't read formlog: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>flock</FONT><FONT color=3D#00ffff>(</FONT>FORMS, =
<FONT color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT>             =
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"can't lock formlog: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT>$query =
=3D CGI-&gt;new<FONT color=3D#00ffff>(</FONT>*FORMS<FONT =
color=3D#00ffff>))</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>last</FONT> <FONT =
color=3D#ffa500>unless</FONT> $query-&gt;param<FONT =
color=3D#00ffff>();</FONT>     <FONT color=3D#bebebe># means end of file
</FONT>    <FONT color=3D#cdcd00>%his_env</FONT> =3D =
$query-&gt;param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>'_environs'</FONT><FONT color=3D#00ffff>);</FONT>
    $count  +=3D $query-&gt;param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>'items requested'</FONT><FONT color=3D#00ffff>)</FONT>
                <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#cdcd00>$his_env</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>REMOTE_HOST</FONT><FONT color=3D#00ffff>}</FONT> =3D~ =
/<FONT color=3D#00ffff>(</FONT>^|\.<FONT =
color=3D#00ffff>)</FONT>perl\.com$/
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Total orders: =
$count\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1049>Program: chemiserie</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/chemiserie">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl -w
# chemiserie - simple CGI shopping for shirts and sweaters
</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>strict</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>CGI</FONT> <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>:standard<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>CGI::Carp</FONT> =
<FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>fatalsToBrowser<FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#cdcd00>%States</FONT><FONT color=3D#00ffff>;</FONT>    	        =
 <FONT color=3D#bebebe># state table mapping pages to functions
</FONT><FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$Current_Screen</FONT><FONT color=3D#00ffff>;</FONT>    	=
<FONT color=3D#bebebe># the current screen
</FONT>
<FONT color=3D#bebebe># Hash of pages and functions.
</FONT>
<FONT color=3D#cdcd00>%States</FONT> =3D <FONT color=3D#00ffff>(</FONT>
    <FONT color=3D#00cd00>'Default'</FONT>     =3D&gt; \&amp;front_page,
    <FONT color=3D#00cd00>'Shirt'</FONT>       =3D&gt; \&amp;shirt,
    <FONT color=3D#00cd00>'Sweater'</FONT>     =3D&gt; \&amp;sweater,
    <FONT color=3D#00cd00>'Checkout'</FONT>    =3D&gt; \&amp;checkout,
    <FONT color=3D#00cd00>'Card'</FONT>        =3D&gt; =
\&amp;credit_card,
    <FONT color=3D#00cd00>'Order'</FONT>       =3D&gt; \&amp;order,
    <FONT color=3D#00cd00>'Cancel'</FONT>      =3D&gt; \&amp;front_page,
<FONT color=3D#00ffff>);</FONT>

$Current_Screen =3D param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>".State"</FONT><FONT color=3D#00ffff>)</FONT> || <FONT =
color=3D#00cd00>"Default"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"No screen for =
$Current_Screen"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#cdcd00>$States</FONT><FONT =
color=3D#00ffff>{</

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -