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

Hello, I want to move from CGI to mod_perl. So I writing wrapper for Apache::Session which will help to avoid code modification uses basic CGI::Session. So I tried to implement "param" function which does not exists in Apache::Session :( I have this sample code dont works:
use strict; use warnings; use Data::Dumper; my $obj = MyTest->new(); #$obj->param('test', 555); #works fine #$obj->param('test2', 55522); #works fine $obj->param('test') = 555; $obj->param('test2') = 55522; my @params = $obj->param(); print Dumper(@params); package MyTest; sub new { bless { 'session' => {} }, shift; } sub param { my $self = shift; my $name = shift; my @val = @_; $self->{session}->{$name} = $val[0] if @_; unless(@val) { return keys %{$self->{session}} unless $name; return $self->{session}->{$name}; } } 1;

Replies are listed 'Best First'.
Re: How to create multipurpose lvalue sub such as CGI::param ?
by kyle (Abbot) on Mar 22, 2008 at 13:02 UTC

    The perlsub section on Lvalue subs warns, "Lvalue subroutines are still experimental and the implementation may change in future versions of Perl." That said, this seems to do what you want:

    package MyTest; sub new { bless { 'session' => {} }, shift; } sub param : lvalue { my $self = shift; my $name = shift; if ( ! defined $name && wantarray ) { my @out = keys %{$self->{session}}; return @out; } $self->{session}->{$name}; } 1;
Re: How to create multipurpose lvalue sub such as CGI::param ?
by dragonchild (Archbishop) on Mar 22, 2008 at 17:59 UTC
    The wonderful thing about opensource is that the source is ... well, open. Go read CGI and see what Lincoln is doing.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      That's just cruel!

        First off, that made me laugh. :-)

        But, it also serves a few very important purposes.

        1. It's opensource for a reason. Leverage that.
        2. param() is one of the most complicated methods in CGI, for a reason - it does a gazillion things. Replicating that isn't simple and it shouldn't be seen as simple.
        3. Lincoln's code is actually quite good. It's the problemspace that sucks. Newbies could do with a worse teacher of style and readability.
        4. Client bugs suck. There's a ton of code in there to deal with browser incompatibilities. Newbies need to be smacked in the face about that, and often.

        There is method to my cruelty.


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: How to create multipurpose lvalue sub such as CGI::param ?
by Anonymous Monk on Mar 22, 2008 at 16:36 UTC
    Why do you need Apache::Session?