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

The following script, running under iis, gets:
"Can't call method "param" on an undefined value at test.pl line 15."
returned to the browser. Line 15 is marked with #####.

Clearly I don't understand how CGI and $CGI::Q works. Can anyone school me on this?

Thank you.
#!/perl/perl.exe -w use strict; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); &main(); sub main{ trimParameters($CGI::Q); print header(), p('Done'); } sub trimParameters{ my $q = shift; foreach ( $q->param() ){ ##### if ($q->param($_)) { $q->param($_,trim($q->param($_))); } } } sub trim{ my ($v) = @_; $v =~ s/^\s*(.*?)\s*$/$1/s; return $v; }

Replies are listed 'Best First'.
Re: Understanding $CGI::Q
by bobn (Chaplain) on Jul 15, 2003 at 23:23 UTC

    from perldoc CGI

    If you import any of the state-maintaining CGI or form-generating meth- ods, a default CGI object will be created and initialized automatically the first time you use any of the methods that require one to be present. This includes param(), textfield(), submit() and the like. (If you need direct access to the CGI object, you can find it in the global variable $CGI::Q). By importing CGI.pm methods, you can create visually elegant scripts:

    But you referred to $CGI::Q before calling any of the methods noted.

    Better to do away with the $CGI::Q and literally create it with:

    my $q = CGI->new;
    after use CGI.... Especially since you're using it in $q->param.) Or do without the object altogether, which you could, because you imported the :standard methods in your use CGI qw/:standard/; statement. But you also are NOT using it for some of your other functions, which is confusing.

    I generally import nothing, create the CGI object $q and use it for all methods. It'sa less confusing to me, even though it adds line noise.

    --Bob Niederman, http://bob-n.com
      Yes yes ... that makes sense. If i put a
      param();
      call before sending $CGI::Q everything works fine.

      Thank you for pointing it out!!

      I'm actually changing the code from the imported "elegant" function interface to the object $q-> interface, as you have suggested, hence the need to handle both situations.

      Thank you all.

Re: Understanding $CGI::Q
by the pusher robot (Monk) on Jul 15, 2003 at 23:15 UTC
    hmm... is there a reason you're not doing (in main) my $q = CGI->new;, and then passing $q to trimParameters?

      yeah.

      I've got code written with the oo CGI interface, and code written with the function CGI interface. trimParameters sits between these two different sets of files. That's why trimParameters takes $q (it ususally sits in a .pm file). It takes $q from the oo code, and $CGI::Q from the function oriented code.

      But, clearly, I do not understand how to use the function oriented CGI module, and how $CGI::Q is initialized.

      By my understanding, if I do my $q = CGI->new, I get two objects, a local object referred to by $q, and a global object that is automatically used in function mode, and can be directly accessed by $CGI::Q. If, on the other hand, I do not do $q=CGI->new, I still get the default cgi object, $CGI::Q

        Sorry, The

        by Anonymous Monk on Jul 15, 2003 at 19:34

        post was me ...