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.


In reply to (Ovid) Re: Re: CGI.pm not returning POSTed param w/mod_perl by Ovid
in thread CGI.pm not returning POSTed param w/mod_perl by voyager

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.