in reply to Question regarding CGI and cookies

Can you tell us the book you're using? It doesn't seem to be one of the standards or someone would have recognised it.

If there's no suggestion at all the the person using this rather simple Shopping Cart script has bought something and set a cookie before, it could still make sense in a way.

It doesn't need a previous cookie to work. It doesn't say

#retrieving cookie @purchases = split(/ /, cookie('cookie')) || die "No cookie!";
after all.

You'll just end up with an array with zero elements. Onto which will be pushed whatever you just bought.

Perhaps it should say

#retrieving cookie if there is one
at that point, if that's the intention?

Anyway. Why would you get a cookie, then turn around right away and set one?

This is an example of a shopping cart, which is a frequently-used metaphor in online shopping. If you go to Amazon or any other online shopping place, what happens? You click to buy a book, and, is the process over at that point? No. You're not asked for your credit card yet. You may choose other books, in the same way as you may use a real shopping cart and select a number of real-life items then go to the checkout.

The imaginary situation is that you've selected some items already, and by using the form, you've added another one to that list of items. Use the form again, and you add another item. Use the form a hundred times and you add a hundred items, and they are all recorded in the cookie.

The weirdness is, it's OK for you to have selected no items yet, the script is intended to work starting from zero items, or any higher number.

That's why you get and set. You get the cookie which says "he has already bought one thing", you add to it and send back a cookie saying "now he has bought two things" and so on.



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re^2: Question regarding CGI and cookies
by JOT007 (Novice) on Nov 09, 2004 at 12:55 UTC
    The text book we are using for this course is CGI/Perl by Diane Zar....It is a Course Technology book by Thomson Learning. This was an exercise in Chapter 11 on cookies and it was the last exercise in the chapter (6). Our instructor could not explain why the order works and he thought like we did, that it should be put in a logical step of creating the cookie, adding to the cookie, sending the cookie to the browser and retrieving the cookie. If we move the lines of script around into the order I just said, then the script will only show the last item clicked and not any previous items if there had been any others clicked before the browser was closed. The script that does work was from his answer sheet with the order that did not make sense to us or him, but it worked. We are just wanted to know how can it create a cookie long before the statement to do it is done? I know this is not a pratical application in real life, but just an assignment to teach us how to create a cookie and to add to it....