![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Thats why there isnt a comma between COUNT and $Counter in the example. If there were, print would treat COUNT as a scalar or list variable and try to write it to the standard output, leading to strange and possibly indecipherable results. In any event, nothing would get into the file to which COUNT refers.
The remaining lines in access.pl write a nice message to the screen and close the file. Nothing new here, right? Now youre ready to turn the program into a CGI application and bring it to the Web! Bringing Your Counter to the WebYou have the rudiments of a Web page access counter written already. You also should have a fair-to-middling understanding of how files work in Perl. However, access.pl will only work from the command line at this point, and we want it to run on the Web. Lets make this basic program CGI-ready. Youve Been Here BeforeYou already know which wrappers need to be fit over a Perl program to turn it into a CGI application. Heres a recap of what it needs to do:
Following these rules, we can fit the wrapper over access.pl. Heres how you do it: #!/perl/bin/perl # access.pl # # Second version. Creates or opens a file with a number # in it, increments the number, writes it back, then displays # the result in a message on a Web page. require "perl-cgi/html.pl"; # Get HTML header, ender. $CountFile = "counter.dat"; # Name of counter file. $PageTitle = "Web Page Access Counter"; # Web page title. # Open the file and read it. If it doesn't exist, its "contents" # will be read into a program variable as "0". open (COUNT, $CountFile); $Counter = <COUNT>; # Read the contents. # Close the file, then reopen it for output. close (COUNT); open (COUNT, ">$CountFile"); # Increment $Counter, then write it back out. Put up a message # with the new value. Close the file. $Counter += 1; print COUNT $Counter; close (COUNT); # Put the result up in a standard HTML document. &HTML_Header ($PageTitle); # HTTP header info. print "<BODY>\n"; print "<H1>$PageTitle</H1>\n"; # Big heading. print "<HR>\n"; # Draw a rule. print "<H3>You are visitor #$Counter "; print "to our Web page!</H3>\n"; &HTML_Ender; # End access.pl Save this as access.pl again, overwriting the old version, and run it, this time as a URL in your Web browser. Figure 4.5 illustrates what you should see.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |