Web, Graphics & Perl/Tk Programming: Best of the Perl Journal Paperback – 20 Mar 2003
|New from||Used from|
- Choose from over 13,000 locations across the UK
- Prime members get unlimited deliveries at no additional cost
- Find your preferred location and add it to your address book
- Dispatch to this address when you check out
Customers Who Bought This Item Also Bought
Enter your mobile number below and we'll send you a link to download the free Kindle App. Then you can start reading Kindle books on your smartphone, tablet, or computer - no Kindle device required.
Getting the download link through email is temporarily not available. Please check back later.
To get the free app, enter your mobile phone number.
About the Author
Jon Orwant, a well-known member of the Perl community, founded The Perl Journal and co-authored O'Reilly's bestseller, Programming Perl, 3rd Edition.
Excerpt. © Reprinted by permission. All rights reserved.
Chapter 4 Cookies
Lincoln D. Stein
A cookie is just a name=value pair,much like the named parameters used in the CGI query string and discussed in CGI Programming .When a web server or CGI script wants to save some state information,it creates a cookie or two and sends them to the browser inside the HTTP header.The browser keeps track ofall the cookies sent to it by a particular server,and stores them in an on-disk database so that the cookies persist even when the browser is closed and reopened later.The next time the browser connects to a web site,it searches its database for all cookies that belong to that server and transmits them back to the server inside the HTTP header.
Cookies can be permanent or set to expire after a number of hours or days.They can be made site-wide,so that the cookie is available to every URL on your site,or restricted to a partial URL path.You can also set a flag in the cookie so that it s only transmitted when the browser and server are communicating via a secure protocol such as SSL.You can even create promiscuous cookies that are sent to every server in a particular Internet domain.
The idea is simple but powerful.If a CGI script needs to save a small amount of state information,such as the user s preferred background color,it can be stored directly in a cookie.If lots of information needs to be stored,you can keep the information in a database on the server s side and use the cookie to record a session key or user ID. Cookies now have their own standard (RFC 2109)and are accepted by all major browsers.
So how do you create a cookie?If you use the CGI.pm library,it s a piece of cake:
2 use CGI qw(:standard);
4 $cookie1 =cookie(-name =>'regular',
5 -value =>'chocolate chip');
6 $cookie2 =cookie(-name =>'high fiber',
7 -value =>'oatmeal raisin');
8 print header(-cookie =>[$cookie1,$cookie2 ]);
Line 2 loads the CGI library and imports the :standard set offunction calls.This allows you to call all ofthe CGI object s methods without explicitly creating a CGI instance a default CGI object is created for you behind the scenes.Lines 4 through 7 create two new cookies using the CGI cookie method.The last step is to incorporate the cookies into the document s HTTP header.We do this in line 8 by printing out the results ofthe header method, passing it the -cookie parameter along with an array reference containing the two cookies.
When we run this script from the command line,the result is:
As you can see,CGI.pm translates each space into %20 ,as the HTTP cookie specification prohibits whitespace and certain other characters such as the semicolon.(It also places an upper limit ofa few kilobytes on the size ofa cookie,so don t try to store the text ofHamlet in one.) When the browser sees these two cookies it squirrels them away and returns them to your script the next time it needs a document from your server.
To retrieve the value ofa cookie sent to you by the browser,use cookie without a value parameter:
2 use CGI qw(:standard);
4 $regular =cookie('regular');
5 $high_fiber =cookie('high fiber');
7 print header(-type =>'text/plain'),
8 "The regular cookie is $regular.\n",
9 "The high fiber cookie is $high_fiber.";
In this example,lines 4 and 5 retrieve the two cookies by name.Lines 7 through 9 print out an HTTP header (containing no cookie this time),and two lines of text.
The output of this script,when viewed in a browser,would be:
The regular cookie is chocolate chip.
The high fiber cookie is oatmeal raisin.
The cookie method is fairly flexible.You can save entire arrays as cookies by giving
the -value parameter an array reference:
$c =cookie(-name =>'specials',
-value =>['oatmeal','chocolate chip','alfalfa']);
This script recognizes four CGI parameters used to change the configuration:
Set the background color.
Set the text color.
Set the size to the indicated value (1 7).
Set the username.
Usually these parameters are sent to the script via the fill out form that it generates, but you could set them from within a URL this way:
/cgi-bin/configure.pl?background=silver &text=blue &name=Stein
Let s walk through the code.Line 2 imports the CGI library,bringing in both the standard method calls and a number ofmethods that generate HTML3-specific tags.Next we define a set of background colors and sizes.The choice of colors may seem capricious,but it s not:These are the background colors defined by the HTML 3.2 standard,and they re based on the original colors used by the IBM VGA graphics display.
Line 9 is where we recover the user s previous preferences,if any.We use the cookie method to fetch a cookie named "preferences ",and store its value in a like-named hash.In lines 12 through 14,we fetch the CGI parameters named text ,background ,name , and size .Ifany ofthem are set,it indicates that the user wants to change the corresponding value saved in the browser s cookie.We store changed parameters in the %preferences hash,replacing the original values.
Line 17 and 18 set the text and background colors to reasonable defaults if they can t be found in either the cookie or the CGI script parameters.
Lines 21 through 25 generate the page s HTTP header.First,we use the cookie method to create the cookie containing the user s preferences.We set the expiration date for the cookie for 30 days in the future so that the cookie will be removed from the browser s database ifthe user doesn t return to this page within that time.We also set the optional -path parameter to .This makes the cookie valid over our entire site so that it s available to every URL the browser fetches.Although we don t take advantage of this yet,it s useful if we later decide that these preferences should have a site-wide effect.Lastly,we emit the HTTP header with the -cookie parameter set.
Most Helpful Customer Reviews on Amazon.com (beta)
- Web programming with Perl
- Graphics programming with Perl
The chapter about "Web programming" covers roughly the following topics:
- CGI programming and CGI.pm
- mod_perl programming
- LWP (libwwwperl) and the modules to parse HTML
- Miscellaneous topics
I found the main chapter "Web programming" very interesting and a good introduction into the topics that it covered.
The chapter "Graphics" is a collection of articles about Graphics programming without the use of Perl/TK (e.g. GD, Perl and GIMP). I found the articles to be quite interesting and to contain some very good ideas. Because I am a database guy, I will not use most of these ideas but I will definitely use GD to generate web graphics. It is always amazing how much you can do with Perl and how easy this can be (sometimes).
The third part of the " Web, Graphics and Perl/TK" deals with Perl/TK only. I personally do not really understand TK (even when I was still using TCL/TK) so that I cannot really comment on the contents of these articles. I found them quite readable and the stile was OK. However if I had to (re-)learn TK, these articles would not be enough. If I need to use Perl/TK, I will buy the O'Reilly books that cover Perl/TK.
I like this TPJ series because the authors cover a certain aspect of Perl or Perl usage in a very condensed form that makes it easy to get a quick start in this area. I am really looking forward to reading "Games, Diversions and Perl Culture" (last book of this series
Recently, I had a similar need for a Perl/Tk program in my current project. The last example shown in the book looked like a good one (creating a MySQL interface with Perl/Tk). However, a large chunk of the core code is missing from the text. I tried to find the code online, but the example code from the O'reilly pages was incomplete (...about 20 lines given). In further searching, I thought that maybe this would be described in the Perl Journals from which the book was created; no luck (BTW: you can find the Perl Journal online, which is also good if you're nostalgic for the old pre- dot.com bubble days).
Overall, it lead me to the conclusion that this book is certainly out-of-date, much like the language. Perl has been stuck trying to move from version 5 to version 6 for a least a decade. Overall, the enthusiasm for Perl is waning it seems. It made me consider if Perl really was the right language for this project.
In summary, if you're looking for something specific for Perl, look on-line...don't buy this book. The amount of resources they have on-line is much better today than 10 years ago. With google, I can find most code answers I'm looking for in 5-10 minutes.
Look for similar items by category
- Books > Computing & Internet > Computer Science > Information Systems
- Books > Computing & Internet > Digital Lifestyle > Online Shopping > Amazon
- Books > Computing & Internet > Programming > Graphics & Multimedia
- Books > Computing & Internet > Programming > Languages
- Books > Computing & Internet > Software & Graphics > Graphics & Multimedia > Image Manipulation & Creation
- Books > Computing & Internet > Web Development > Web Design > Applications
- Books > Computing & Internet > Web Development > Web Design > Web Graphics & Animation
- Books > Computing & Internet > Web Development > Web Scripting & Programming