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

I am using HTML::Template, CGI::Application CGI::Session to set and use session variables and it works great. However, I need a better solution than the one below for setting the default value and to check if I need to set the variable. The code works ok, but as the number of session variables increase so does this ugly piece of code.
if($post{'page'}){ $self->session->param('page' => $post{'page'}); } elsif( (!$post{'page'}) && (!$self->session->param('page')) ) { $self->session->param('page' => 1); } else { $self->session->param('page' => 1); }
I know I can set the default value on the TMPL_VAR's but I can't apply that to all variables. Anyone have a cleaner solution?

Replies are listed 'Best First'.
Re: setting session variables
by tlm (Prior) on May 18, 2005 at 10:29 UTC

    You could simplify your code's appearance by defining a utility function like

    sub maybe_set_param { my ( $self, $param, $default, $value ) = @_; $self->session->param( $param => defined $value ? $value : $default +); }
    Then you'd replace the param-setting if-statement with
    $self->maybe_set_param( page => $post{ page }, 1 );
    Or, if you always get your param values from this %post hash, then you can simplify things a bit further like this:
    sub maybe_set_param { my ( $self, $param, $default ) = @_; my $value = $self->post->{ $param }; $self->session->param( $param => defined $value ? $value : $default +); } # ... $self->maybe_set_param( page => 1 );
    In the last version, I used an accessor post defined to return a ref to the %post hash.

    And from here you can simplify things even further, by making maybe_set_param capable of handling multiple params in one call:

    sub maybe_set_param { my $self = shift; my %defaults = @_; my $post = $self->post; while ( my ( $param, $default ) = each %defaults ) { my $value = $post->{ $param }; $self->session->param( $param => defined $value ? $value : $defaul +t ); } }
    Now you can set various parameters at once like this:
    $self->maybe_set_param( page => 1, foo => 15, bar => -1 );

    the lowliest monk

      now you're talking...thanks alot
Re: setting session variables
by Thilosophy (Curate) on May 18, 2005 at 07:19 UTC
    Why do you need if/else, when both are doing the same? Is this
    elsif( (!$post{'page'}) && (!$self->session->param('page')) ) { $self->session->param('page' => 1); } else { $self->session->param('page' => 1); }
    not the same as
    else{ $self->session->param('page' => 1); }
    ?

    And

    if($post{'page'}){ ... } elsif( (!$post{'page'}) && ...) {
    also looks redundant.

    How about this:

    my $session = $self->session; foreach (qw[ page ] ) { $session->param( $_ => $post{$_} ? $post{$_} : 1); }
    This also scales nicely with an increasing number of session variables (if all of them default to 1 ).
      Your right, it's redudant.
      Pretty much what I want to achieve is the possibility to set a default value to my session variables, it could for example be todays year, month and day, or a boolean value, or a name.

      The session variable should be changed if the variable exists and is not empty in the querystring.

      Most of my session variables don't default to 1

      Maybe I am on the wrong track here.

Re: setting session variables
by rjsaulakh (Beadle) on May 26, 2005 at 10:34 UTC
    can you please help me with the cgi script used for handling session variables