📄 cgi.3
字号:
\& open (OUT,">>test.out") || die;\& $records = 5;\& foreach (0..$records) {\& my $q = new CGI;\& $q\->param(\-name=>\*(Aqcounter\*(Aq,\-value=>$_);\& $q\->save(\e*OUT);\& }\& close OUT;\&\& # reopen for reading\& open (IN,"test.out") || die;\& while (!eof(IN)) {\& my $q = new CGI(\e*IN);\& print $q\->param(\*(Aqcounter\*(Aq),"\en";\& }.Ve.PPThe file format used for save/restore is identical to that used by theWhitehead Genome Center's data exchange format \*(L"Boulderio\*(R", and can bemanipulated and even databased using Boulderio utilities. See.PP.Vb 1\& http://stein.cshl.org/boulder/.Ve.PPfor further details..PPIf you wish to use this method from the function-oriented (non-OO)interface, the exported name for this method is \fB\f(BIsave_parameters()\fB\fR..Sh "\s-1RETRIEVING\s0 \s-1CGI\s0 \s-1ERRORS\s0".IX Subsection "RETRIEVING CGI ERRORS"Errors can occur while processing user input, particularly whenprocessing uploaded files. When these errors occur, \s-1CGI\s0 will stopprocessing and return an empty parameter list. You can test forthe existence and nature of errors using the \fI\fIcgi_error()\fI\fR function.The error messages are formatted as \s-1HTTP\s0 status codes. You can eitherincorporate the error text into an \s-1HTML\s0 page, or use it as the valueof the \s-1HTTP\s0 status:.PP.Vb 8\& my $error = $q\->cgi_error;\& if ($error) {\& print $q\->header(\-status=>$error),\& $q\->start_html(\*(AqProblems\*(Aq),\& $q\->h2(\*(AqRequest not processed\*(Aq),\& $q\->strong($error);\& exit 0;\& }.Ve.PPWhen using the function-oriented interface (see the next section),errors may only occur the first time you call \fI\fIparam()\fI\fR. Be readyfor this!.Sh "\s-1USING\s0 \s-1THE\s0 FUNCTION-ORIENTED \s-1INTERFACE\s0".IX Subsection "USING THE FUNCTION-ORIENTED INTERFACE"To use the function-oriented interface, you must specify which \s-1CGI\s0.pmroutines or sets of routines to import into your script's namespace.There is a small overhead associated with this importation, but itisn't much..PP.Vb 1\& use CGI <list of methods>;.Ve.PPThe listed methods will be imported into the current package; you cancall them directly without creating a \s-1CGI\s0 object first. This exampleshows how to import the \fB\f(BIparam()\fB\fR and \fB\f(BIheader()\fB\fRmethods, and then use them directly:.PP.Vb 3\& use CGI \*(Aqparam\*(Aq,\*(Aqheader\*(Aq;\& print header(\*(Aqtext/plain\*(Aq);\& $zipcode = param(\*(Aqzipcode\*(Aq);.Ve.PPMore frequently, you'll import common sets of functions by referringto the groups by name. All function sets are preceded with a \*(L":\*(R"character as in \*(L":html3\*(R" (for tags defined in the \s-1HTML\s0 3 standard)..PPHere is a list of the function sets you can import:.IP "\fB:cgi\fR" 4.IX Item ":cgi"Import all CGI-handling methods, such as \fB\f(BIparam()\fB\fR, \fB\f(BIpath_info()\fB\fRand the like..IP "\fB:form\fR" 4.IX Item ":form"Import all fill-out form generating methods, such as \fB\f(BItextfield()\fB\fR..IP "\fB:html2\fR" 4.IX Item ":html2"Import all methods that generate \s-1HTML\s0 2.0 standard elements..IP "\fB:html3\fR" 4.IX Item ":html3"Import all methods that generate \s-1HTML\s0 3.0 elements (such as<table>, <super> and <sub>)..IP "\fB:html4\fR" 4.IX Item ":html4"Import all methods that generate \s-1HTML\s0 4 elements (such as<abbrev>, <acronym> and <thead>)..IP "\fB:netscape\fR" 4.IX Item ":netscape"Import all methods that generate Netscape-specific \s-1HTML\s0 extensions..IP "\fB:html\fR" 4.IX Item ":html"Import all HTML-generating shortcuts (i.e. 'html2' + 'html3' +\&'netscape')....IP "\fB:standard\fR" 4.IX Item ":standard"Import \*(L"standard\*(R" features, 'html2', 'html3', 'html4', 'form' and 'cgi'..IP "\fB:all\fR" 4.IX Item ":all"Import all the available methods. For the full list, see the \s-1CGI\s0.pmcode, where the variable \f(CW%EXPORT_TAGS\fR is defined..PPIf you import a function name that is not part of \s-1CGI\s0.pm, the modulewill treat it as a new \s-1HTML\s0 tag and generate the appropriatesubroutine. You can then use it like any other \s-1HTML\s0 tag. This is toprovide for the rapidly-evolving \s-1HTML\s0 \*(L"standard.\*(R" For example, sayMicrosoft comes out with a new tag called <gradient> (which causes theuser's desktop to be flooded with a rotating gradient fill until hismachine reboots). You don't need to wait for a new version of \s-1CGI\s0.pmto start using it immediately:.PP.Vb 2\& use CGI qw/:standard :html3 gradient/;\& print gradient({\-start=>\*(Aqred\*(Aq,\-end=>\*(Aqblue\*(Aq});.Ve.PPNote that in the interests of execution speed \s-1CGI\s0.pm does \fBnot\fR usethe standard Exporter syntax for specifying load symbols. This maychange in the future..PPIf you import any of the state-maintaining \s-1CGI\s0 or form-generatingmethods, a default \s-1CGI\s0 object will be created and initializedautomatically the first time you use any of the methods that requireone to be present. This includes \fB\f(BIparam()\fB\fR, \fB\f(BItextfield()\fB\fR,\&\fB\f(BIsubmit()\fB\fR and the like. (If you need direct access to the \s-1CGI\s0object, you can find it in the global variable \fB\f(CB$CGI::Q\fB\fR). Byimporting \s-1CGI\s0.pm methods, you can create visually elegant scripts:.PP.Vb 10\& use CGI qw/:standard/;\& print \& header,\& start_html(\*(AqSimple Script\*(Aq),\& h1(\*(AqSimple Script\*(Aq),\& start_form,\& "What\*(Aqs your name? ",textfield(\*(Aqname\*(Aq),p,\& "What\*(Aqs the combination?",\& checkbox_group(\-name=>\*(Aqwords\*(Aq,\& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],\& \-defaults=>[\*(Aqeenie\*(Aq,\*(Aqmoe\*(Aq]),p,\& "What\*(Aqs your favorite color?",\& popup_menu(\-name=>\*(Aqcolor\*(Aq,\& \-values=>[\*(Aqred\*(Aq,\*(Aqgreen\*(Aq,\*(Aqblue\*(Aq,\*(Aqchartreuse\*(Aq]),p,\& submit,\& end_form,\& hr,"\en";\&\& if (param) {\& print \& "Your name is ",em(param(\*(Aqname\*(Aq)),p,\& "The keywords are: ",em(join(", ",param(\*(Aqwords\*(Aq))),p,\& "Your favorite color is ",em(param(\*(Aqcolor\*(Aq)),".\en";\& }\& print end_html;.Ve.Sh "\s-1PRAGMAS\s0".IX Subsection "PRAGMAS"In addition to the function sets, there are a number of pragmas thatyou can import. Pragmas, which are always preceded by a hyphen,change the way that \s-1CGI\s0.pm functions in various ways. Pragmas,function sets, and individual functions can all be imported in thesame \fIuse()\fR line. For example, the following use statement imports thestandard set of functions and enables debugging mode (pragma\&\-debug):.PP.Vb 1\& use CGI qw/:standard \-debug/;.Ve.PPThe current list of pragmas is as follows:.IP "\-any" 4.IX Item "-any"When you \fIuse \s-1CGI\s0 \-any\fR, then any method that the query objectdoesn't recognize will be interpreted as a new \s-1HTML\s0 tag. This allowsyou to support the next \fIad hoc\fR Netscape or Microsoft \s-1HTML\s0extension. This lets you go wild with new and unsupported tags:.Sp.Vb 3\& use CGI qw(\-any);\& $q=new CGI;\& print $q\->gradient({speed=>\*(Aqfast\*(Aq,start=>\*(Aqred\*(Aq,end=>\*(Aqblue\*(Aq});.Ve.SpSince using <cite>any</cite> causes any mistyped method nameto be interpreted as an \s-1HTML\s0 tag, use it with care or not atall..IP "\-compile" 4.IX Item "-compile"This causes the indicated autoloaded methods to be compiled up front,rather than deferred to later. This is useful for scripts that runfor an extended period of time under FastCGI or mod_perl, and forthose destined to be crunched by Malcolm Beattie's Perl compiler. Useit in conjunction with the methods or method families you plan to use..Sp.Vb 1\& use CGI qw(\-compile :standard :html3);.Ve.Spor even.Sp.Vb 1\& use CGI qw(\-compile :all);.Ve.SpNote that using the \-compile pragma in this way will always havethe effect of importing the compiled functions into the currentnamespace. If you want to compile without importing use the\&\fIcompile()\fR method instead:.Sp.Vb 2\& use CGI();\& CGI\->compile();.Ve.SpThis is particularly useful in a mod_perl environment, in which youmight want to precompile all \s-1CGI\s0 routines in a startup script, andthen import the functions individually in each mod_perl script..IP "\-nosticky" 4.IX Item "-nosticky"By default the \s-1CGI\s0 module implements a state-preserving behaviorcalled \*(L"sticky\*(R" fields. The way this works is that if you areregenerating a form, the methods that generate the form field valueswill interrogate \fIparam()\fR to see if similarly-named parameters arepresent in the query string. If they find a like-named parameter, theywill use it to set their default values..SpSometimes this isn't what you want. The \fB\-nosticky\fR pragma preventsthis behavior. You can also selectively change the sticky behavior ineach element that you generate..IP "\-tabindex" 4.IX Item "-tabindex"Automatically add tab index attributes to each form field. With thisoption turned off, you can still add tab indexes manually by passing a\&\-tabindex option to each field-generating method..IP "\-no_undef_params" 4.IX Item "-no_undef_params"This keeps \s-1CGI\s0.pm from including undef params in the parameter list..IP "\-no_xhtml" 4.IX Item "-no_xhtml"By default, \s-1CGI\s0.pm versions 2.69 and higher emit \s-1XHTML\s0(http://www.w3.org/TR/xhtml1/). The \-no_xhtml pragma disables thisfeature. Thanks to Michalis Kabrianis <kabrianis@hellug.gr> for thisfeature..SpIf \fIstart_html()\fR's \-dtd parameter specifies an \s-1HTML\s0 2.0 or 3.2 \s-1DTD\s0, \&\s-1XHTML\s0 will automatically be disabled without needing to use this pragma..IP "\-nph" 4.IX Item "-nph"This makes \s-1CGI\s0.pm produce a header appropriate for an \s-1NPH\s0 (noparsed header) script. You may need to do other things as wellto tell the server that the script is \s-1NPH\s0. See the discussionof \s-1NPH\s0 scripts below..IP "\-newstyle_urls" 4.IX Item "-newstyle_urls"Separate the name=value pairs in \s-1CGI\s0 parameter query strings withsemicolons rather than ampersands. For example:.Sp.Vb 1\& ?name=fred;age=24;favorite_color=3.Ve.SpSemicolon-delimited query strings are always accepted, but will not beemitted by \fIself_url()\fR and \fIquery_string()\fR unless the \-newstyle_urlspragma is specified..SpThis became the default in version 2.64..IP "\-oldstyle_urls" 4.IX Item "-oldstyle_urls"Separate the name=value pairs in \s-1CGI\s0 parameter query strings withampersands rather than semicolons. This is no longer the default..IP "\-autoload" 4.IX Item "-autoload"This overrides the autoloader so that any function in your programthat is not recognized is referred to \s-1CGI\s0.pm for possible evaluation.This allows you to use all the \s-1CGI\s0.pm functions without adding them toyour symbol table, which is of concern for mod_perl users who areworried about memory consumption. \fIWarning:\fR when\&\fI\-autoload\fR is in effect, you cannot use \*(L"poetry mode\*(R"(functions without the parenthesis). Use \fI\fIhr()\fI\fR ratherthan \fIhr\fR, or add something like \fIuse subs qw/hr p header/\fR to the top of your script..IP "\-no_debug" 4.IX Item "-no_debug"This turns off the command-line processing features. If you want torun a \s-1CGI\s0.pm script from the command line to produce \s-1HTML\s0, and youdon't want it to read \s-1CGI\s0 parameters from the command line or \s-1STDIN\s0,then use this pragma:.Sp.Vb 1\& use CGI qw(\-no_debug :standard);.Ve.IP "\-debug" 4.IX Item "-debug"This turns on full debugging. In addition to reading \s-1CGI\s0 argumentsfrom the command-line processing, \s-1CGI\s0.pm will pause and try to readarguments from \s-1STDIN\s0, producing the message \*(L"(offline mode: entername=value pairs on standard input)\*(R" features..SpSee the section on debugging for more details..IP "\-private_tempfiles" 4.IX Item "-private_tempfiles"\&\s-1CGI\s0.pm can process uploaded file. Ordinarily it spools the uploadedfile to a temporary directory, then deletes the file when done.However, this opens the risk of eavesdropping as described in the fileupload section. Another \s-1CGI\s0 script author could peek at this dataduring the upload, even if it is confidential information. On Unixsystems, the \-private_tempfiles pragma will cause the temporary fileto be unlinked as soon as it is opened and before any data is writteninto it, reducing, but not eliminating the risk of eavesdropping(there is still a potential race condition). To make life harder forthe attacker, the program chooses tempfile names by calculating a 32bit checksum of the incoming \s-1HTTP\s0 headers..SpTo ensure that the temporary file cannot be read by other \s-1CGI\s0 scripts,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -