£26.50
FREE Delivery in the UK.
Only 2 left in stock (more on the way).
Dispatched from and sold by Amazon. Gift-wrap available.
Quantity:1
Web, Graphics & Perl/Tk P... has been added to your Basket
Have one to sell?
Flip to back Flip to front
Listen Playing... Paused   You're listening to a sample of the Audible audio edition.
Learn more
See this image

Web, Graphics & Perl/Tk Programming: Best of the Perl Journal Paperback – 20 Mar 2003


See all formats and editions Hide other formats and editions
Amazon Price
New from Used from
Kindle Edition
"Please retry"
Paperback
"Please retry"
£26.50
£23.00 £27.33
Note: This item is eligible for click and collect. Details
Pick up your parcel at a time and place that suits you.
  • Choose from over 13,000 locations across the UK
  • Prime members get unlimited deliveries at no additional cost
How to order to an Amazon Pickup Location?
  1. Find your preferred location and add it to your address book
  2. Dispatch to this address when you check out
Learn more
£26.50 FREE Delivery in the UK. Only 2 left in stock (more on the way). Dispatched from and sold by Amazon. Gift-wrap available.
click to open popover

Special Offers and Product Promotions

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.

  • Apple
  • Android
  • Windows Phone

To get the free app, enter your mobile phone number.




Product details

Product Description

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.

Creating Cookies
So how do you create a cookie?If you use the CGI.pm library,it ’s a piece of cake:
0 #!/usr/bin/perl
1
2 use CGI qw(:standard);
3
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:
Set-cookie:regular=chocolate%20chip
Set-cookie:high%20fiber=oatmeal%20raisin
Content-type:text/html

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.

Retrieving Cookies
To retrieve the value ofa cookie sent to you by the browser,use cookie without a –value parameter:
0 #!/usr/bin/perl
1
2 use CGI qw(:standard);
3
4 $regular =cookie('regular');
5 $high_fiber =cookie('high fiber');
6
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:
background
Set the background color.
text
Set the text color.
size
Set the size to the indicated value (1 –7).
name
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.

Customer Reviews

There are no customer reviews yet on Amazon.co.uk.
5 star
4 star
3 star
2 star
1 star

Most Helpful Customer Reviews on Amazon.com (beta)

Amazon.com: HASH(0x9587ae94) out of 5 stars 3 reviews
HASH(0x9589fc24) out of 5 stars Good TPJ articles for the topics Web, Graphics and TK 2 Sept. 2003
By Gregor Theis - Published on Amazon.com
Format: Paperback
"Web, Graphics and Perl/TK" is a reprint of the best articles of the The Perl Journal (TPJ) for the following topics:
- Web programming with Perl
- Graphics programming with Perl
- Perl/TK
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
HASH(0x95b07fe4) out of 5 stars Outdated, broken examples 15 Oct. 2013
By Rex Barker - Published on Amazon.com
Format: Paperback Verified Purchase
I admit that I actually bought this book around 9 years ago in 2004. At the time, I was interested in some Perl/Tk programming overview which the book covers in short detail (less than 1/3 of the book).

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.
HASH(0x957111c8) out of 5 stars Like Wine 3 July 2015
By efrankamo - Published on Amazon.com
Format: Paperback Verified Purchase
Although dated, I use this to type out the examples so I can wire myself up to write Perl code...
Were these reviews helpful? Let us know


Feedback