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

hi everyone, I'm new in mod_perl. I rewrited the code of program from book. But, the code doesn't work. Please, help me to find error lines... :
package HandleCGIData2; # file: HandleCGIData2.pm # use statements, including # our new Apache2::Request module use strict; use Apache2::Const ':common'; use Apache2::Request; sub handler { # shift the argument and pass it into # the new() method in the Apache2::Request # class my $r = Apache2::Request->new(shift); # we can now call param() with the Apache2::Request # object in a similar way to using CGI.pm my $name = $r->param('name') || 'John Doe'; my $age = $r->param('age') || '50'; # set the content type and send the header $r->content_type('text/html'); # $r->send_http_header(); # print the HTML, including our variables # $name and $age $r->print(<<EOHTML); <html> <head> <title>Using Apache2::RequestIO</title> </head> <body bgcolor="#ffffff"> <h1>Using Apache2::Request</h1> name = $name<br> age = $age </body> </html> EOHTML return OK; } 1;

Replies are listed 'Best First'.
Re: I can't find error section of code
by Xilman (Hermit) on Jun 20, 2010 at 07:28 UTC

    Hint: you will tend to receive much more assistance if you report more than "it doesn't work".

    In what way does it not work? What are the symptoms? What do you expect it to do which it does not, and what does it do which you expect it not to do?

    Get back with more details and I may be able to help you.

    Paul

Re: I can't find error section of code
by rowdog (Curate) on Jun 20, 2010 at 16:41 UTC

    This is where you go wrong...

    my $r = Apache2::Request->new(shift);

    You're throwing away the Apache2::RequestRec to build an Apache2::Request which is used for parsing parameters rather than setting content type and such. You want to keep both objects. Maybe try something like...

    sub handler { my $r = shift; my $req = Apache2::Request->new($r); my $name = $req->param('name') || 'John Doe'; $r->content_type('text/html'); $r->print(<<EOHTML); <html>...</html> EOHTML return OK; }

    If you haven't seen it yet, the mod_perl web site is an excellent source of documentation and tutorials.

Re: I can't find error section of code
by Khen1950fx (Canon) on Jun 20, 2010 at 12:09 UTC
    Since you want to handle CGI, I tried a different way:
    package HandleCGIData2; use strict; use warnings; use Apache qw(exit); print &handler; sub handler { local $^W = 1; #!/usr/bin/perl use CGI; my $q = CGI->new; my $them = $q->remote_host; my $name = 'John Doe'; my $age = '50'; print $q->header( 'text/html' ), $q->start_html(-title => 'My CGI Example'); print <<EOT; <html> <head> <title>HandleCGI</title> </head> <body bgcolor="#ffffff"> <h1>Using name and age</h1> name = $name<br> age = $age </body> </html> EOT print $q->end_html; }