Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

CGI::Application and $self->param() wierdness

by Anonymous Monk
on Jul 07, 2004 at 22:53 UTC ( [id://372592]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks! I've run into yet another C::A puzzler. Specifically, I can create a $self->param() value in my CGI script and retrieve it in any non-setup() method. Why can't I retrieve the value in the setup() method? The docs say:
This function is a good place to define properties specific to your application via the $webapp->param() method.
Granted the doc example given only shows creation of properties rather than retrieval, but why shouldn't it work? Thanks in advance.

WebApp.cgi

#!/usr/local/bin/perl use WebApp; my $webapp = WebApp->new(); $webapp->param( 'var1' => 'val1' ); $webapp->param( 'var2' => 'val2' ); $webapp->run;

WebApp.pm

package WebApp; use base 'CGI::Application'; use strict; use warnings; my ( $var1, $var2 ); sub setup { my $self = shift; $self->start_mode( 'my_page' ); $self->run_modes([ 'my_page' ]); $var1 = $self->param( 'var1' ); } sub my_page { my $self = shift; $var2 = $self->param( 'var2' ); return "<p>var1: '$var1'</p>\n" . "<p>var2: '$var2'</p>\n"; } 1;

output

<p>var1: ''</p> <p>var2: 'val2'</p>

Replies are listed 'Best First'.
Re: CGI::Application and $self->param() wierdness
by samtregar (Abbot) on Jul 08, 2004 at 03:15 UTC
    Because setup() is called by new() and you're not setting the params until new() returns. Pass them to new() via PARAMS and you'll see them in setup().

    -sam

      Thank you. I feel kind of silly. I was familiar with the PARAMS option on the new method, but didn't realize the significance/difference. Works fine after modification.
Re: CGI::Application and $self->param() wierdness
by PodMaster (Abbot) on Jul 07, 2004 at 23:03 UTC
    See CGI::Application::Loop, but basically
    package HI; use base qw[ CGI::Application ]; sub setup { warn "I AM SETUP!!!!!!!!!!!!!!!!!!" } package main; my $hi = HI->new(); warn "Got myself a HI, now I gotta RUN IT!!!!!!!!!!!!!!!!!"; $hi->run; __END__ I AM SETUP!!!!!!!!!!!!!!!!!! at - line 3. Got myself a HI, now I gotta RUN IT!!!!!!!!!!!!!!!!! at - line 6. No such run mode 'start' at - line 7

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: CGI::Application and $self->param() wierdness
by dragonchild (Archbishop) on Jul 08, 2004 at 12:58 UTC
    You're using global variables. This is stupid. If you want to use the 'var1' parameter in the my_page() function, grab it there. If you want to use it in the setup() function, grab it there. If you want to use it in both places, grab it in both places. This smacks to me of premature optimization.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      While I appreciate your concern, I thought it was pretty clear that the example I gave was contrived for debugging purposes and to illustrate the problem as clearly/simply as possible.

      Yes, my actual application will only grab it where I need to use it. I was just concerned that I couldn't grab the value in the setup() method.

      For the sake of completeness (of the contrived example, not my real code), the following is the altered WebApp.cgi:

      #!/usr/local/bin/perl use WebApp; my $webapp = WebApp->new( PARAMS => { 'var1' => 'val1', 'var2' => 'val2', }, ); $webapp->run;
      I'm not recommending that people use global variables this way in production code.
        To address this issue, the reason it didn't work is because setup() is called from new(). So, your param() calls initially were after the call to new(), so they weren't executed prior to setup() being called. That's the correct solution to the problem.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://372592]
Approved by jlongino
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found