Cgi perl program




















This might be something simple like a page counter, or a complex form-handler. Shopping carts and e-commerce sites are driven by CGI programs. So are ad banners; they keep track of who has seen and clicked on an ad. CGI programs may be written in any programming language; we're just using Perl because it's fairly easy to learn. If you're already an expert in some other language and are just reading to get the basics, here it is: if you're writing a CGI that's going to generate an HTML page, you must include this statement somewhere in the program before you print out anything else:.

This is a content-type header that tells the receiving web browser what sort of data it is about to receive — in this case, an HTML document. If you forget to include it, or if you print something else before printing this header, you'll get an "Internal Server Error" when you try to access the CGI program.

Now let's try writing a simple CGI program. Enter the following lines into a new file, and name it "first. Note that even though the lines appear indented on this page, you do not have to indent them in your file. The first line!

The subsequent lines can start in any column. Save or upload the file into your web directory, then chmod first. You will have to do this every time you create a new program; however, if you're editing an existing program, the permissions will remain the same and shouldn't need to be changed again.

You should see a web page with "Hello, world! Let's try another example. Start a new file or if you prefer, edit your existing first. It's up to your program to print out all of the HTML you want to display in the visitor's browser, so you'll have to include print statements for every HTML tag:.

Save this file, adjust the file permissions if necessary, and view it in your web browser. This time you should see "Hello, world! Now not only have you learned to write your first CGI program, you've also learned your first Perl statement, the print function:.

This function will write out any string, variable, or combinations thereof to the current output channel. In the case of your CGI program, the current output is being printed to the visitor's browser. Newlines are not required, but they will make your program's output easier to read. You can write multiple lines of text without using multiple print statements by using the here-document syntax:. You can use any word or phrase for the end marker you'll see an example next where we use "EndOfHTML" as the marker ; just be sure that the closing marker matches the opening marker exactly it is case-sensitive , and also that the closing marker is on a line by itself, with no spaces before or after the marker.

When a closing here-document marker is on the last line of the file, be sure you have a line break after the marker. If the end-of-file mark is on the same line as the here-doc marker, you'll get an error when you run your program. Perl offers a powerful feature to programmers: add-on modules.

These are collections of pre-written code that you can use to do all kinds of tasks. You can save yourself the time and trouble of reinventing the wheel by using these modules. Some modules are included as part of the Perl distribution; these are called standard library modules and don't have to be installed. Control Flow. OOP Concepts. Regular Expressions.

File Handling. CGI Programming. Table of Contents. Improve Article. Save Article. Like Article. Previous Perl Useful File-handling functions. Recommended Articles. Just about every Linux distribution comes with a version of Apache which is by far the most common web server on the Internet. You can use just about any language to write CGI programs, but the majority of people seem to use Perl.

Here it is in its entirety. If not, read the boxout section on debugging CGI programs to get help tracking down the problem.

The first of these is the -T option on the shebang line. The second difference is the addition of the line that prints the content type header. This is our first brush with the CGI protocol. In most cases the only header that we will need to return is the content type header. This tells the browser what kind of data the program is sending.

The set of headers should be separated from the actual data with a blank line. This is achieved by printing two newline characters after the final or, in this case, only header. That could just as easily be done with an HTML page. Each time you reload the script the time will be updated. The added complexity comes purely from the fact that we have to display a complete HTML page. Also note that we have changed the content type to reflect the fact that we are now returning HTML.

The browser will recognise the content type and will convert the HTML into a web page. Here is the output from this script.

In the standard Perl library there is a module called CGI. Here is the same script using CGI. This loads the CGI. We then use five of these functions to display the page. You should still use CGI. In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics. Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie.

If it does, it is stored as a plain text record on the visitor's hard drive. Now, when the visitor arrives at another page on your site, the cookie is available for retrieval. If this is blank, the cookie will expire when the visitor quits the browser. This may be blank if you want to retrieve the cookie from any directory or page. If this field is blank, no such restriction exists.

This is very easy to send cookies to browser. Assuming you want to set UserID and Password as cookies. From this example you must have understood how to set cookies. Here it is optional to set cookies attributes like Expires, Domain, and Path.

This is very easy to retrieve all the set cookies. You will find many built-in modules over the internet which provide you direct functions to use in your CGI program.

Following are the important once. CGI Module. Devi Killada.



0コメント

  • 1000 / 1000