steveAZ98 has asked for the wisdom of the Perl Monks concerning the following question:
and the test script. Note: I ran this with an undef cid the first time and then I entered the cid so that I was recalling the same session each time.package Item; $VERSION = 1.0; use strict; use Class::MethodMaker new_with_init => 'new', new_hash_init => '_init_args', static_hash => 'counter', get_set => [qw( sku qty )]; sub count { $_[0]->counter('count') } sub _incr_count { $_[0]->counter(count=>$_[0]->counter(' +count')+1) } sub _decr_count { $_[0]->counter(count=>$_[0]->counter('count')- +1) } sub init { my ($self, %args) = @_; $self->_init_args(%args); $self->_incr_count(); return $self; } sub DESTROY { $_[0]->_decr_count(); } package Info; $VERSION = 1.0; use strict; use Apache::Session::MySQL; use Class::MethodMaker "-sugar"; make methods new_with_init => 'new', object_list => [ Items => 'items' ], get_set => [qw( orderid )]; sub timestamp { $_[0]->{_timestamp} = time() } sub cid { $_[0]->{_session_id} } sub init { my ($self, %args) = @_; tie %{ $self }, 'Apache::Session::MySQL', $args{cid}, { DataSource => 'dbi:mysql:ssdw_store', UserName => 'web', Password => 'webpass1', LockDataSource => 'dbi:mysql:ssdw_store', LockUserName => 'web', LockPassword => 'webpass1', }; $self->timestamp; return $self; } sub DESTROY { $_[0]->timestamp; untie %{$_[0]}; } 1;
Thanks Again.#!/usr/bin/perl use lib "."; use Info; use Item; my $id = '3c85ccbef20f62dd495e9b9b958bf040'; my $info = Info->new('cid'=>$id); print "Cid: ", $info->cid, "\n"; if ($ARGV[0]) { $info->clear_items(); $info->push_items(Item->new(sku=>'123123',qty=>1)); $info->push_items(Item->new(sku=>'123321',qty=>2)); } print $info->count_items(), "\n"; while ($x = $info->pop_items) { print "Sku: ", $x->sku, " Qty: ", $x->qty, "\n"; } exit;
|
|---|