Quoting Scott Prelewicz <[EMAIL PROTECTED]>:

> After days of frustration, I must turn to the list for some help. Below
> is my code for the start of a rewrite of a shopping cart. I am rewriting
> it to CGI::App with CGI::App:Sess. I don't know what Im doing wrong and
> about to give up. I cat get the hash ref that the cart is in to store in
> the session table. See anything?
> 
[SNIP]
>     # Extract the shopping cart from the session, if there isn't one yet
> create it
>     if (!defined ($cart_ref=$self->session->param("cart"))) {
>             # The cart is a hash reference of items indexd by item id
>           $cart_ref={};

I think this is probably where things went wrong.  Your newly created $cart_ref
will go out of scope right away.  you need to save this empty hash in the cart,
since you assume later on the the code that you will get a hashref when looking
for the cart in the session.

            # Create an empty cart in the session
            $self->session->param("cart" => {});

>     }
> }
> 
> 
> sub teardown {
>     my $self=shift;
> 
>     # If the session still exists, store the cart back into it    
>     my $cart_count=0;
>     my $session=$self->session;
>     my $cart_ref=$session->param('cart');
>     if (defined($session)) {
>         $session->param("cart", $cart_ref);
>         $session->close();
>     }

The above only loads the value of cart from the session and then saves it right
back again.  I don't think that will accomplish what you want.

May I suggest an alternative strategy for handling your cart.  In your super
class create a function called cart:

sub cart {
  my $self = shift;

  if (!$self->{__CART}) {
    $self->{__CART} = $self->session->param('cart') || {};
  }
  return $self->{__CART};
}

then in your teardown method add the following line

  if ($self->{__CART}) {
    $self->session->param('cart' => $self->{__CART});
  }

Now, anywhere in your code you can call  

  $cart_ref = $self->cart();

to get access to your cart reference, and any changes you make to $cart_ref
should be stored back in the session during the teardown stage.  It should
simplify the cart handling code a bit, and it should be more efficient, since
you don't have to keep accessing the session object everytime you access the cart.

I haven't checked through all of the code you provided, but I think the above
changes should solve your problem.  If it doesn't please respond back and we can
try something different.

Cheers,

Cees

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[EMAIL PROTECTED]/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to