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

Thanks for the comments so far. While I haven't solved my problem yet, I have learned more about CGI.pm. My script:
#! /perl/bin/perl -w use strict; use CGI(); use DBI; use Data::Dumper; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use LC::SchemaClassMaker; use LC::DBMaint; my $file_in = '/web/lc/perllib/schema.xm'; my $drh = 'dbi:CSV:f_dir=/web/lc/database'; my $dbh = DBI->connect($drh); my $cm = LC::SchemaClassMaker->new({ schema_filename => $file_in , base_class => 'LC::PersistentSQL' , dbh => $dbh , class_prefix => 'XT' }); my $tm = LC::DBMaint->new({ dbc_classname => 'XT::book' }); $tm->set_attr('default', 'widgets', { review => {widget => 'scrolling_list'} , author => {widget => 'scrolling_list'} }); $tm->execute; my $header_args = $tm->get_cookie ? {-cookie => $tm->get_cookie} : {}; print CGI::header($header_args), CGI::start_html(); print $tm->default_html(); print CGI::end_html();
There is are no references to CGI.pm in LC::SchemaClassMaker.

LC::DBMaint does a use CGI(); and makes several non-OO calls (CGI::funcname), including calls to param, cookie, script_name, script and various HTML-generating methods.

This module also instantiates several XT::book objects whose class are generated by SchemaClassMaker and whose methods are all inherited from a base class, LC::PersistentSQL (which has no CGI references) but inherits from a class LC::Persistent which is similar to LC::DBMaint in its use of CGI: use CGI; and several non-OO calls to param and HTML-generating functions.

(In the event you see typos, etc. in the above, this does work fine under vanilla CGI scripting.

Replies are listed 'Best First'.
(Ovid) Re: Re: CGI.pm not returning POSTed param w/mod_perl
by Ovid (Cardinal) on Jun 13, 2001 at 21:45 UTC

    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.