bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians,

In a CGI::App module, I have

$self->session->param('promo_item' => 'member');

but I want to add (aka 'push') an element to an array ref, on the fly, to achieve, in effect:

$self->session->param('promo_item' => [ 'member','visitor']);

Tried stuff like

$self->session->param('promo_item')->[1] = 'visitor';

but, of course, no dice. Ideas? Thanks in advance.


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: How to add an element to a session param
by rhesa (Vicar) on Apr 21, 2007 at 00:40 UTC
    my $pi = $self->session->param('promo_item'); unless( ref $pi eq 'ARRAY' ) { $pi = [ $pi ]; } push @$pi, 'visitor'; $self->session->param('promo_item' => $pi );
Re: How to add an element to a session param
by bobf (Monsignor) on Apr 21, 2007 at 02:42 UTC

    Perhaps the append method will do what you want. From the CGI docs (see "Setting the value(s) of a named parameter"):

    $query->param('foo','an','array','of','values');
    This sets the value for the named parameter 'foo' to an array of values. This is one way to change the value of a field AFTER the script has been invoked once before.

    param() also recognizes a named parameter style of calling described in more detail later:
    $query->param(-name=>'foo',-values=>['an','array','of','values']);

    As a bonus, you don't have to break object encapsulation. :-)

Re: How to add an element to a session param
by NetWallah (Canon) on Apr 21, 2007 at 00:37 UTC
    Try
    push (@{$self->session->param{promo_item}}, 'member','visitor');
    Update: jettero alerted me to the fact that Session::param was a method (I verified by RTFS)- probably not accessible as a hash. Please refer to the more correct post by bobf, below.

         "Choose a job you like and you will never have to work a day of your life" - Confucius