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

Edit:   I fixed it.

I have, as you know, a legacy app that used mod_perl and therefore a Apache2::RequestRec and a Apache2::Request.

Exactly what did that module return for my $scalar = $request->param('foo'); if 'foo' did not exist?   Was it undef, or was it an empty string?   The reason I ask, is that the original code apparently assumed empty-string.

(Large amounts of whining deleted...)

The problem here is that param is a very complicated function.   It can be called with zero parameters or one, and in an array context or a scalar context.   The existing code simply did this:

has 'plack_request' ...
handles
param => 'param',
...
... and it Just Works.™

(More whimpering deleted ...)

Thanks to the clue given in post 853728, I solved the problem, I think:

around 'param' => sub { my $orig = shift; my $self = shift; my @result = $self->$orig(@_); if (wantarray) { return @result; } else { return defined($result[0]) ? $result[0] : ''; } };

Replies are listed 'Best First'.
Re: [SOLVED] Subtle Q: what does 'Apache2::RequestRec' return for missing "param()"?
by ikegami (Patriarch) on Jun 08, 2011 at 18:15 UTC
    handles => { param => 'param', },

    is effectively

    handles => { param => sub { my $plack_request = shift->plack_request; return $pack_request->param(@_); }, },

    So instead of using around, you could use

    handles => { param => sub { my $plack_request = shift->plack_request; return (wantarray ? $plack_request->param(@_) : $plack_request->param(@_) // '' ); }, },

    One less layers of subs, and less action at a distance.

Re: [SOLVED] Subtle Q: what does 'Apache2::RequestRec' return for missing "param()"?
by locked_user sundialsvc4 (Abbot) on Jun 08, 2011 at 21:48 UTC

    Excellent!   Thank you!

    (Assuming "||" for your "//".)   perldoc perlop

    Needs Perl 5.10 though.

    After initial effusion, I wound up keeping after because it can be applied to a qw() list of attributes which leverages just one bit of code for all of them.