The main differences are that we now need to import a set of functions into our name space usually the "standard" functions , and we don't need to create the CGI object.
The examples in this document mainly use the object-oriented style. Most CGI. To simplify this interface, all routines use a named argument calling style that looks like this:. Each argument name is preceded by a dash. In fact, only the first argument needs to begin with a dash. If a dash is present in the first argument CGI. Several routines are commonly called with just one argument. In the case of these routines you can provide the single argument without an argument name.
In this case, the single argument is the document type. Sometimes named arguments expect a scalar, sometimes a reference to an array, and sometimes a reference to a hash. Often, you can pass any type of argument and the routine will do whatever is most appropriate. For example, the param routine is used to set a CGI parameter to a single or a multi-valued value.
The two cases are shown below:. Many routines will do something useful with a named argument that it doesn't recognize. For example, you can produce non-standard HTTP header fields by providing them as named arguments:. If you provide a file handle to the new method, it will read parameters from the file or STDIN, or whatever.
The file can be in any of the forms describing below under debugging i. Conveniently, this type of file is created by the save method see below. Multiple records can be saved and restored. Perl purists will be pleased to know that this syntax accepts references to file handles, or even references to filehandle globs, which is the "official" way to pass a filehandle. This will re initialize the default CGI object from the indicated file handle.
If the script was invoked as the result of an ISINDEX search, the parsed keywords can be obtained as an array using the keywords method. If the script was invoked with a parameter list e. The array of parameter names returned will be 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 part of the spec, and so isn't guaranteed. When calling param If the parameter is multivalued e. Otherwise the method will return the first value. Warning - calling param in list context can lead to vulnerabilities if you do not sanitise user input as it is possible to inject other param keys and values into your code.
The following code is an example of a vulnerability as the call to param will be evaluated in list context and thus possibly inject extra keys and values into the hash:. If you call param in list context with an argument a warning will be raised by CGI.
If the parameter does not exist at all, then param will return undef in scalar context, and the empty list in a list context. This sets the value for the named parameter 'foo' to an array of values. This adds a value or list of values to the named parameter. The values are appended to the end of the parameter if it already exists. Otherwise the parameter is created. Note that this method only recognizes the named argument calling syntax.
This creates a series of variables in the 'R' namespace. For keyword lists, a variable R::keywords will appear. If no namespace is given, this method will assume 'Q'. NOTE 1: Variable names are transformed as necessary into legal perl variable names. All non-legal characters are transformed into underscores. If you need to keep the original names, you should use the param method instead to access CGI variables by name.
In fact, you should probably not use this method at all given the above caveats and security risks. This completely clears a list of parameters.
It sometimes useful for resetting parameters that you don't want passed down between script invocations. If you are using the function call interface, use "Delete " instead to avoid conflicts with perl's built-in delete operator.
This clears the CGI object completely. It might be useful to ensure that all the defaults are taken when you create a fill-out form. To retrieve it, use code like this:. If you don't know what the preceding means, worry not. This will return an array reference to the named parameter, which you then can manipulate in any way you like. Many people want to fetch the entire parameter list as a hash in which the keys are the names of the CGI parameters, and the values are the parameters' values.
The Vars method does this. Called in a scalar context, it returns the parameter list as a tied hash reference. Changing a key changes the value of the parameter in the underlying CGI parameter list.
Called in a list context, it returns the parameter list as an ordinary hash. This allows you to read the contents of the parameter list, but not to change it. When using this, the thing you must watch out for are multivalued CGI parameters.
You must split this packed string in order to get at the individual values. This is the convention introduced long ago by Steve Brenner in his cgi-lib.
This will write the current state of the form to the provided filehandle. You can read it back in by providing a filehandle to the new method. Note that the filehandle can be a file, a pipe, or whatever. Both name and value are URL escaped. Multi-valued CGI parameters are represented as repeated names. You can write out multiple records and read them back in with several calls to new. You can do this across several sessions by opening the file in append mode, allowing you to create primitive guest books, or to keep a history of users' queries.
Here's a short example of creating multiple session records:. See Boulder for further details. Errors can occur while processing user input, particularly when processing uploaded files. When these errors occur, CGI will stop processing and return an empty parameter list. The error messages are formatted as HTTP status codes. You can either incorporate the error text into a page, or use it as the value of the HTTP status:.
When using the function-oriented interface see the next section , errors may only occur the first time you call param. Be ready for this! To use the function-oriented interface, you must specify which CGI. There is a small overhead associated with this importation, but it isn't much. The listed methods will be imported into the current package; you can call them directly without creating a CGI object first.
This example shows how to import the param and header methods, and then use them directly:. More frequently, you'll import common sets of functions by referring to the groups by name. All function sets are preceded with a ":" character as in ":cgi" for CGI protocol handling methods.
Import all the available methods. For the full list, see the CGI. Note that in the interests of execution speed CGI. This may change in the future. In addition to the function sets, there are a number of pragmas that you can import. Pragmas, which are always preceded by a hyphen, change the way that CGI. Pragmas, function sets, and individual functions can all be imported in the same use line. For example, the following use statement imports the cgi set of functions and enables debugging mode pragma -debug :.
This makes CGI. If you do not use this option you can manually select which fields are expected to return utf-8 strings and convert them using code like this:. You may need to do other things as well to tell the server that the script is NPH. See the discussion of NPH scripts below. For example:. This is no longer the default. This turns off the command-line processing features. If you want to run a CGI. This turns on full debugging. Most of CGI. Generally you will produce the HTTP header first, followed by the document itself.
Each of these functions produces a fragment of HTTP which you can print out directly so that it is processed by the browser, appended to a string, or saved to a file for later use. This tells the browser what type of document to expect, and gives other optional information, such as the language, expiration date, and whether to cache the document.
The header can also be manipulated for special purposes, such as server push and pay per view pages. An optional second parameter specifies the status code and a human-readable message. For example, you can specify , "No response" to create a script that tells the browser to do nothing at all. Note that RFC expects the human-readable phase to be there as well as the numeric status code. The last example shows the named argument style for passing arguments to the CGI methods using named parameters.
Recognized parameters are -type , -status , -expires , and -cookie. Any other named parameters will be stripped of their initial hyphens and turned into header fields, allowing you to specify any HTTP header you desire. Internal underscores will be turned into hyphens:. Most browsers will not cache the output from CGI scripts. Every time the browser reloads the page, the script is invoked anew.
You can change this behavior with the -expires parameter. When you specify an absolute or relative expiration interval with this parameter, some browsers and proxy servers will cache the script's output until the indicated expiration date. The following forms are all valid for the -expires field:.
The -cookie parameter generates a header that tells the browser to provide a "magic cookie" during all subsequent transactions with your script. Some cookies have a special format that includes interesting attributes such as expiration time. Use the cookie method to create and retrieve session cookies. The -nph parameter, if set to a true value, will issue the correct headers to work with a NPH no-parse-header script.
This is important to use with certain servers that expect all their scripts to be NPH. The -charset parameter can be used to control the character set sent to the browser. If not provided, defaults to ISO As a side effect, this sets the charset method as well. Note that the default being ISO may not make sense for all content types, e. The -attachment parameter can be used to turn the page into an attachment. Instead of displaying the page, some browsers will prompt the user to save it to disk.
The value of the argument is the suggested name for the saved file. The -p3p parameter will add a P3P tag to the outgoing header. The parameter can be an arrayref or a space-delimited string of P3P tags. Invalid multi-line header input will trigger in an exception.
When multi-line headers are received, CGI. Sometimes you don't want to produce a document yourself, but simply redirect the browser elsewhere, perhaps choosing a URL based on the time of day or the identity of the user. The redirect method redirects the browser to a different URL. If you use redirection like this, you should not print out a header as well. You are advised to use full URLs absolute with respect to current URL or even including the http: or ftp: part in redirection requests as relative URLs are resolved by the user agent of the client so may not do what you want or expect them to do.
All names arguments recognized by header are also recognized by redirect. However, most HTTP headers, including those generated by -cookie and -target, are ignored by the browser. The -status parameter will set the status of the redirect. HTTP defines several different possible redirection status codes, and the default if not specified is , which means "moved temporarily. Note that the human-readable phrase is also expected to be present to conform with RFC , section 6. This is most useful when you want to jump around within the document using internal anchors but you don't want to disrupt the current contents of the form s.
Something like this will do the trick:. Called without any arguments, it returns the full form of the URL, including host name and port number. Produce a relative URL. This is useful if you want to re-invoke your script with different parameters. Produce the full URL, exactly as if called without any arguments. This overrides the -relative and -absolute arguments. Append the additional path information to the URL. This can be combined with -full , -absolute or -relative.
Append the query string to the URL. Use it in the same way as param. The main difference is that it allows you to read the parameters, but not set them. If you try to mix a URL query string with a form submitted with the GET method, the results will not be what you expect. When the form is processed, you can retrieve an IO::File compatible handle for a file upload field like this:.
In a list context, upload will return an array of filehandles. This makes it possible to process forms that use the same name for multiple upload fields. Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine.
Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI. When a file is uploaded the browser usually sends along some information along with it in the format of headers. The information usually includes the MIME content type. To retrieve this information, call uploadInfo. It returns a reference to a hash containing all the document headers. If you are using a machine that recognizes "text" and "binary" data modes, be sure to understand when and how to use them see the Camel book.
Otherwise you may find that binary files are corrupted during file uploads. When processing an uploaded file, CGI. Come for the solution, stay for everything else. Welcome to our community! I dont know why, in my RedHat 8. I did install perl5.
Anyone know how to install it? I looked in cpan, it seems it's default perl module. And I cant find any installer for it. Because I saw in my other redhat 8. Kelly Black. Join our community to see this answer! Unlock 1 Answer and 4 Comments. Andrew Hancock - VMware vExpert. See if this solution works for you by signing up for a 7 day free trial. What do I get with a subscription? With your subscription - you'll gain access to our exclusive IT community of thousands of IT pros.
We can't always guarantee that the perfect solution to your specific problem will be waiting for you.
0コメント