📄 cgi.3
字号:
Use another name for the argument, if one is available. For example, \-value is an alias for \-values..IP "2." 4Change the capitalization, e.g. \-Values.IP "3." 4Put quotes around the argument name, e.g. '\-values'.PPMany routines will do something useful with a named argument that itdoesn't recognize. For example, you can produce non-standard \s-1HTTP\s0header fields by providing them as named arguments:.PP.Vb 4\& print $q\->header(\-type => \*(Aqtext/html\*(Aq,\& \-cost => \*(AqThree smackers\*(Aq,\& \-annoyance_level => \*(Aqhigh\*(Aq,\& \-complaints_to => \*(Aqbit bucket\*(Aq);.Ve.PPThis will produce the following nonstandard \s-1HTTP\s0 header:.PP.Vb 5\& HTTP/1.0 200 OK\& Cost: Three smackers\& Annoyance\-level: high\& Complaints\-to: bit bucket\& Content\-type: text/html.Ve.PPNotice the way that underscores are translated automatically intohyphens. HTML-generating routines perform a different type oftranslation..PPThis feature allows you to keep up with the rapidly changing \s-1HTTP\s0 and\&\s-1HTML\s0 \*(L"standards\*(R"..Sh "\s-1CREATING\s0 A \s-1NEW\s0 \s-1QUERY\s0 \s-1OBJECT\s0 (OBJECT-ORIENTED \s-1STYLE\s0):".IX Subsection "CREATING A NEW QUERY OBJECT (OBJECT-ORIENTED STYLE):".Vb 1\& $query = new CGI;.Ve.PPThis will parse the input (from both \s-1POST\s0 and \s-1GET\s0 methods) and storeit into a perl5 object called \f(CW$query\fR..PPAny filehandles from file uploads will have their position reset to the beginning of the file..Sh "\s-1CREATING\s0 A \s-1NEW\s0 \s-1QUERY\s0 \s-1OBJECT\s0 \s-1FROM\s0 \s-1AN\s0 \s-1INPUT\s0 \s-1FILE\s0".IX Subsection "CREATING A NEW QUERY OBJECT FROM AN INPUT FILE".Vb 1\& $query = new CGI(INPUTFILE);.Ve.PPIf you provide a file handle to the \fInew()\fR method, it will readparameters from the file (or \s-1STDIN\s0, or whatever). The file can be inany of the forms describing below under debugging (i.e. a series ofnewline delimited TAG=VALUE pairs will work). Conveniently, this typeof file is created by the \fIsave()\fR method (see below). Multiple recordscan be saved and restored..PPPerl purists will be pleased to know that this syntax acceptsreferences to file handles, or even references to filehandle globs,which is the \*(L"official\*(R" way to pass a filehandle:.PP.Vb 1\& $query = new CGI(\e*STDIN);.Ve.PPYou can also initialize the \s-1CGI\s0 object with a FileHandle or IO::Fileobject..PPIf you are using the function-oriented interface and want toinitialize \s-1CGI\s0 state from a file handle, the way to do this is with\&\fB\f(BIrestore_parameters()\fB\fR. This will (re)initialize thedefault \s-1CGI\s0 object from the indicated file handle..PP.Vb 3\& open (IN,"test.in") || die;\& restore_parameters(IN);\& close IN;.Ve.PPYou can also initialize the query object from an associative arrayreference:.PP.Vb 4\& $query = new CGI( {\*(Aqdinosaur\*(Aq=>\*(Aqbarney\*(Aq,\& \*(Aqsong\*(Aq=>\*(AqI love you\*(Aq,\& \*(Aqfriends\*(Aq=>[qw/Jessica George Nancy/]}\& );.Ve.PPor from a properly formatted, URL-escaped query string:.PP.Vb 1\& $query = new CGI(\*(Aqdinosaur=barney&color=purple\*(Aq);.Ve.PPor from a previously existing \s-1CGI\s0 object (currently this clones theparameter list, but none of the other object-specific fields, such asautoescaping):.PP.Vb 2\& $old_query = new CGI;\& $new_query = new CGI($old_query);.Ve.PPTo create an empty query, initialize it from an empty string or hash:.PP.Vb 1\& $empty_query = new CGI("");\&\& \-or\-\&\& $empty_query = new CGI({});.Ve.Sh "\s-1FETCHING\s0 A \s-1LIST\s0 \s-1OF\s0 \s-1KEYWORDS\s0 \s-1FROM\s0 \s-1THE\s0 \s-1QUERY:\s0".IX Subsection "FETCHING A LIST OF KEYWORDS FROM THE QUERY:".Vb 1\& @keywords = $query\->keywords.Ve.PPIf the script was invoked as the result of an <\s-1ISINDEX\s0> search, theparsed keywords can be obtained as an array using the \fIkeywords()\fR method..Sh "\s-1FETCHING\s0 \s-1THE\s0 \s-1NAMES\s0 \s-1OF\s0 \s-1ALL\s0 \s-1THE\s0 \s-1PARAMETERS\s0 \s-1PASSED\s0 \s-1TO\s0 \s-1YOUR\s0 \s-1SCRIPT:\s0".IX Subsection "FETCHING THE NAMES OF ALL THE PARAMETERS PASSED TO YOUR SCRIPT:".Vb 1\& @names = $query\->param.Ve.PPIf the script was invoked with a parameter list(e.g. \*(L"name1=value1&name2=value2&name3=value3\*(R"), the \fIparam()\fR methodwill return the parameter names as a list. If the script was invokedas an <\s-1ISINDEX\s0> script and contains a string without ampersands(e.g. \*(L"value1+value2+value3\*(R") , there will be a single parameter named\&\*(L"keywords\*(R" containing the \*(L"+\*(R"\-delimited keywords..PP\&\s-1NOTE:\s0 As of version 1.5, the array of parameter names returned willbe in the same order as they were submitted by the browser.Usually this order is the same as the order in which the parameters are defined in the form (however, this isn't partof the spec, and so isn't guaranteed)..Sh "\s-1FETCHING\s0 \s-1THE\s0 \s-1VALUE\s0 \s-1OR\s0 \s-1VALUES\s0 \s-1OF\s0 A \s-1SINGLE\s0 \s-1NAMED\s0 \s-1PARAMETER:\s0".IX Subsection "FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:".Vb 1\& @values = $query\->param(\*(Aqfoo\*(Aq);\&\& \-or\-\&\& $value = $query\->param(\*(Aqfoo\*(Aq);.Ve.PPPass the \fIparam()\fR method a single argument to fetch the value of thenamed parameter. If the parameter is multivalued (e.g. from multipleselections in a scrolling list), you can ask to receive an array. Otherwisethe method will return a single value..PPIf a value is not given in the query string, as in the queries\&\*(L"name1=&name2=\*(R" or \*(L"name1&name2\*(R", it will be returned as an emptystring. This feature is new in 2.63..PPIf the parameter does not exist at all, then \fIparam()\fR will return undefin a scalar context, and the empty list in a list context..Sh "\s-1SETTING\s0 \s-1THE\s0 \s-1VALUE\s0(S) \s-1OF\s0 A \s-1NAMED\s0 \s-1PARAMETER:\s0".IX Subsection "SETTING THE VALUE(S) OF A NAMED PARAMETER:".Vb 1\& $query\->param(\*(Aqfoo\*(Aq,\*(Aqan\*(Aq,\*(Aqarray\*(Aq,\*(Aqof\*(Aq,\*(Aqvalues\*(Aq);.Ve.PPThis sets the value for the named parameter 'foo' to an array ofvalues. This is one way to change the value of a field \s-1AFTER\s0the script has been invoked once before. (Another way is withthe \-override parameter accepted by all methods that generateform elements.).PP\&\fIparam()\fR also recognizes a named parameter style of calling describedin more detail later:.PP.Vb 1\& $query\->param(\-name=>\*(Aqfoo\*(Aq,\-values=>[\*(Aqan\*(Aq,\*(Aqarray\*(Aq,\*(Aqof\*(Aq,\*(Aqvalues\*(Aq]);\&\& \-or\-\&\& $query\->param(\-name=>\*(Aqfoo\*(Aq,\-value=>\*(Aqthe value\*(Aq);.Ve.Sh "\s-1APPENDING\s0 \s-1ADDITIONAL\s0 \s-1VALUES\s0 \s-1TO\s0 A \s-1NAMED\s0 \s-1PARAMETER:\s0".IX Subsection "APPENDING ADDITIONAL VALUES TO A NAMED PARAMETER:".Vb 1\& $query\->append(\-name=>\*(Aqfoo\*(Aq,\-values=>[\*(Aqyet\*(Aq,\*(Aqmore\*(Aq,\*(Aqvalues\*(Aq]);.Ve.PPThis adds a value or list of values to the named parameter. Thevalues are appended to the end of the parameter if it already exists.Otherwise the parameter is created. Note that this method onlyrecognizes the named argument calling syntax..Sh "\s-1IMPORTING\s0 \s-1ALL\s0 \s-1PARAMETERS\s0 \s-1INTO\s0 A \s-1NAMESPACE:\s0".IX Subsection "IMPORTING ALL PARAMETERS INTO A NAMESPACE:".Vb 1\& $query\->import_names(\*(AqR\*(Aq);.Ve.PPThis creates a series of variables in the 'R' namespace. For example,\&\f(CW$R::foo\fR, \f(CW@R:foo\fR. For keyword lists, a variable \f(CW@R::keywords\fR will appear.If no namespace is given, this method will assume 'Q'.\&\s-1WARNING:\s0 don't import anything into 'main'; this is a major securityrisk!!!!.PP\&\s-1NOTE\s0 1: Variable names are transformed as necessary into legal Perlvariable names. All non-legal characters are transformed intounderscores. If you need to keep the original names, you should usethe \fIparam()\fR method instead to access \s-1CGI\s0 variables by name..PP\&\s-1NOTE\s0 2: In older versions, this method was called \fB\f(BIimport()\fB\fR. As of version 2.20, this name has been removed completely to avoid conflict with the built-inPerl module \fBimport\fR operator..Sh "\s-1DELETING\s0 A \s-1PARAMETER\s0 \s-1COMPLETELY:\s0".IX Subsection "DELETING A PARAMETER COMPLETELY:".Vb 1\& $query\->delete(\*(Aqfoo\*(Aq,\*(Aqbar\*(Aq,\*(Aqbaz\*(Aq);.Ve.PPThis completely clears a list of parameters. It sometimes useful forresetting parameters that you don't want passed down between scriptinvocations..PPIf you are using the function call interface, use \*(L"\fIDelete()\fR\*(R" insteadto avoid conflicts with Perl's built-in delete operator..Sh "\s-1DELETING\s0 \s-1ALL\s0 \s-1PARAMETERS:\s0".IX Subsection "DELETING ALL PARAMETERS:".Vb 1\& $query\->delete_all();.Ve.PPThis clears the \s-1CGI\s0 object completely. It might be useful to ensurethat all the defaults are taken when you create a fill-out form..PPUse \fIDelete_all()\fR instead if you are using the function call interface..Sh "\s-1HANDLING\s0 NON-URLENCODED \s-1ARGUMENTS\s0".IX Subsection "HANDLING NON-URLENCODED ARGUMENTS"If POSTed data is not of type application/x\-www\-form\-urlencoded ormultipart/form\-data, then the POSTed data will not be processed, butinstead be returned as-is in a parameter named \s-1POSTDATA\s0. To retrieveit, use code like this:.PP.Vb 1\& my $data = $query\->param(\*(AqPOSTDATA\*(Aq);.Ve.PP(If you don't know what the preceding means, don't worry about it. Itonly affects people trying to use \s-1CGI\s0 for \s-1XML\s0 processing and otherspecialized tasks.).Sh "\s-1DIRECT\s0 \s-1ACCESS\s0 \s-1TO\s0 \s-1THE\s0 \s-1PARAMETER\s0 \s-1LIST:\s0".IX Subsection "DIRECT ACCESS TO THE PARAMETER LIST:".Vb 2\& $q\->param_fetch(\*(Aqaddress\*(Aq)\->[1] = \*(Aq1313 Mockingbird Lane\*(Aq;\& unshift @{$q\->param_fetch(\-name=>\*(Aqaddress\*(Aq)},\*(AqGeorge Munster\*(Aq;.Ve.PPIf you need access to the parameter list in a way that isn't coveredby the methods above, you can obtain a direct reference to it bycalling the \fB\f(BIparam_fetch()\fB\fR method with the name of the . Thiswill return an array reference to the named parameters, which you thencan manipulate in any way you like..PPYou can also use a named argument style using the \fB\-name\fR argument..Sh "\s-1FETCHING\s0 \s-1THE\s0 \s-1PARAMETER\s0 \s-1LIST\s0 \s-1AS\s0 A \s-1HASH:\s0".IX Subsection "FETCHING THE PARAMETER LIST AS A HASH:".Vb 4\& $params = $q\->Vars;\& print $params\->{\*(Aqaddress\*(Aq};\& @foo = split("\e0",$params\->{\*(Aqfoo\*(Aq});\& %params = $q\->Vars;\&\& use CGI \*(Aq:cgi\-lib\*(Aq;\& $params = Vars;.Ve.PPMany people want to fetch the entire parameter list as a hash in whichthe keys are the names of the \s-1CGI\s0 parameters, and the values are theparameters' values. The \fIVars()\fR method does this. Called in a scalarcontext, it returns the parameter list as a tied hash reference.Changing a key changes the value of the parameter in the underlying\&\s-1CGI\s0 parameter list. Called in a list context, it returns theparameter list as an ordinary hash. This allows you to read thecontents of the parameter list, but not to change it..PPWhen using this, the thing you must watch out for are multivalued \s-1CGI\s0parameters. Because a hash cannot distinguish between scalar andlist context, multivalued parameters will be returned as a packedstring, separated by the \*(L"\e0\*(R" (null) character. You must split thispacked string in order to get at the individual values. This is theconvention introduced long ago by Steve Brenner in his cgi\-lib.plmodule for Perl version 4..PPIf you wish to use \fIVars()\fR as a function, import the \fI:cgi\-lib\fR set offunction calls (also see the section on CGI-LIB compatibility)..Sh "\s-1SAVING\s0 \s-1THE\s0 \s-1STATE\s0 \s-1OF\s0 \s-1THE\s0 \s-1SCRIPT\s0 \s-1TO\s0 A \s-1FILE:\s0".IX Subsection "SAVING THE STATE OF THE SCRIPT TO A FILE:".Vb 1\& $query\->save(\e*FILEHANDLE).Ve.PPThis will write the current state of the form to the providedfilehandle. You can read it back in by providing a filehandleto the \fInew()\fR method. Note that the filehandle can be a file, a pipe,or whatever!.PPThe format of the saved file is:.PP.Vb 5\& NAME1=VALUE1\& NAME1=VALUE1\*(Aq\& NAME2=VALUE2\& NAME3=VALUE3\& =.Ve.PPBoth name and value are \s-1URL\s0 escaped. Multi-valued \s-1CGI\s0 parameters arerepresented as repeated names. A session record is delimited by asingle = symbol. You can write out multiple records and read themback in with several calls to \fBnew\fR. You can do this across severalsessions by opening the file in append mode, allowing you to createprimitive guest books, or to keep a history of users' queries. Here'sa short example of creating multiple session records:.PP.Vb 1\& use CGI;\&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -