in reply to Re: CGI.pm not returning POSTed param w/mod_perl
in thread CGI.pm not returning POSTed param w/mod_perl

Hmm... in this case, you appear to have gotten very lucky. You are using CGI.pm in a manner inconsistent with the documentation, but because of the way it's designed, it still works.

CGI.pm can be used with either a function oriented (FO) or object oriented (OO) manner.


use CGI qw/:standard/; # this is FO print header, start_html('some title'), ...

use CGI; # this is OO my $q = CGI->new; print $q->header, $q->start_html('some title'), ...

Your code, however, tries to use the FO interface without having CGI.pm exporting any functions or variables! Typically, when trying to use the FO methods, if you have forgotten to ask CGI.pm to export those functions, Perl will complain:

C:\>perl -MCGI -e "print header();" Undefined subroutine &main::header called at -e line 1.

However, you have explictly asked for the header() function in the CGI namespace:

C:\>perl -MCGI -e "print CGI::header();" Content-Type: text/html; charset=ISO-8859-1

Well, why does that work? Typically, when we call an object method, the first argument passed is the object whose method we're using. CGI.pm, however, does things a slightly different way:

sub header { my($self,@p) = self_or_default(@_);

The subroutine &self_or_default will return a reference to a CGI object, plus the original parameter list, regardless of whether you use:


  • $q->header; # OO
  • header(); # FO, exported to your program's namespace
  • CGI::header; # FO, still in the CGI namespace

In other words, you have used the CGI module in an undocumented manner, but it works because of the way Lincoln Stein wrote the module. Reading through the code, I can't find any reason why this would be a problem, but I wouldn't guarantee that it's not. If you use a module in a fashion other than documented, all bets are off.

The main reason I would be concerned about your usage is that the module uses caller quite a bit. If you have different namespaces using the FO interface, CGI.pm may not pass data around correctly. Again, this is a matter of my not seeing all of your code and not reading through all of the CGI.pm code. Use the OO interface. Much cleaner and more predictable, for complicated needs.

Incidentally, because of the way this module is written (and because of the way you are using is) the first call to CGI::header() is reading the data from STDIN, though this shouldn't be a problem. However, because your code is a bit odd, I wouldn't guarantee that this is not an issue. If your other modules are using the OO interface, as I mentioned before, make sure you instantiate them with an empty parameter set, if you don't need the params:

my $q = CGI->new({});

As far as I can tell from reading the code and the documentation, you cannot protect the contents of STDIN if using the FO interface. Without seeing all of your code (and not knowing a great deal about mod_perl), I'd recommend that you convert everything to the OO interface. Since you are using mod_perl, I suspect that the FO interface is a bad idea since you'd be exporting a bunch of function to the namespace that mod_perl creates for you. No need to do that and have them hogging memory.

Last comment: since you do not appear to need to read STDIN in this snippet, where are you reading from it? It gets wiped out with your call to &CGI::header (at least, it should. I'm looking at $CGI::VERSION 2.74). If any of your code tries to read it after this, you're out of luck.

Cheers,
Ovid

PS: Think that was enough rambling? :)

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.