Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Debugging when using Apache-request;

by IOrdy (Friar)
on Jul 30, 2001 at 09:51 UTC ( [id://100795]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, I'm kinda new to perl and I would like to know how I would go about debugging code when using Apache.

I was just looking at the error.log for apache but once I got all the errors sorted apache just segfaults when I try to run my script.

How do I find out what is going on? (I dont think it's apache because I tested individual bits of code as I went and my problems only started when I tried to make them all work together)



Here is the code:
#!/usr/bin/perl -w # Woot! The second version. $VERSION = 0.02; # like a good little coder use strict; # required modules use Apache; use XML::DOM; use HTML::Template; use vars qw($parsed_xml); # move this sort of stuff to a .conf file later my $xml_file = '/home/iordy/perl.iordy.net/iXML_base.xml'; # Read and parse the document my $parser = XML::DOM::Parser->new(); $parsed_xml = $parser->parsefile ($xml_file); ## main ## # &process_args(); #process arguments and move on from there. my $page_content = "page content"; &showpage($page_content); ## subs ## sub search { my ($search_for_section) = @_; my @search_section_results; foreach my $node ($parsed_xml->getElementsByTagName('section_id')){ if ($node->getAttibute('section_name') eq $search_for_section) { push @search_section_results,$node->getAttribute('section_id'); } } return @search_section_results; } sub process_args { my $r = Apache->request; my %arguments = $r->args; my $page_content; foreach my $key (keys %arguments) { my $arg = $arguments{$key}; #swap this to a switch later? if ($arg eq 'search') { foreach my $search_result (&search($arg)){ $page_content .= $search_result . "<br />"; } } else{ $page_content = "Page Error: No valid argument specified!"; } } &showpage($page_content); } # note: make this a package later on, plus add in all the other attrib +utes that a page will need. sub showpage { my ($page_content) = @_; my $r = Apache->request; $r->send_http_header('text/html'); $r->print($page_content); }



And here is the xml:
<xml> <section section_id=1 section_name="root" section_title="iordy.com -> +root"> <page_content> <page>root.html</page> </page_content> </section> <section section_id=2 section_name="art" section_title="iordy.com -> a +rt"> <page_content> <page>art.html</page> <page page_title="iordy.com -> art.2">art2.html</page> </page_content> </section> <section section_id=3 section_name="test" section_title="iordy.com -> +subtree test"> <page_content> <page page_title="iordy.com -> subtree test page1">test1.html</page> <page page_title="iordy.com -> subtree test page2">test2.html</page> </page_content> <section section_id=4 section_name="test3" section_title="iordy.com - +> subtree test2"> <page_content> <page page_title="iordy.com -> subtree test page 3">test3.html</pag +e> </page_content> </section> </section> </xml>

Replies are listed 'Best First'.
Re: Debugging when using Apache-request;
by chromatic (Archbishop) on Jul 30, 2001 at 11:16 UTC
    I usually just use print statements for debugging. (That's what Larry does; he said as much last week.)
    do_this(); print STDERR "Did this!\n"; do_that(); print STDERR "Did that!\n";
    There's room to get more creative, but I'm pretty happy with the code in one window and a tail -f error.log in the other.

    My suspicion is that there's a conflict between Apache's built-in Expat and the Expat required by XML::DOM. It bit me once. You'd have to recompile Apache and disable the Expat target to test that, however.

Re: Debugging when using Apache-request;
by busunsl (Vicar) on Jul 30, 2001 at 18:16 UTC
    You can debug CGI-scripts with the Tk-Debugger by Andrew Page.

    Look here for a quick review and here for the module.
    CGI debugging is explained at the end of the documentation.

Re: Debugging when using Apache-request;
by thatguy (Parson) on Jul 30, 2001 at 11:54 UTC
    you may also want to try

    use CGI::Carp qw(fatalsToBrowser);

    puts a nice err in the browser window so you don't have to dig through the logs. (of course I could be totally off on what you're wanting..)

    Update: oh yeah.. thats a wrong answer.. i can't get my modules installed correctly to give a right answer. sorry. -p

Re: Debugging when using Apache-request;
by mugwumpjism (Hermit) on Jul 30, 2001 at 18:05 UTC

    Also, you may have problems if you have mod_perl and PHP compiled into the same Apache binary, in particular when loading XS modules; which is documented somewhere deep in the mod_perl docs.

    My advice is to use FastCGI, takes the Perl out of the web server and makes debugging easier too.

Re: Debugging when using Apache-request;
by spudzeppelin (Pilgrim) on Jul 31, 2001 at 01:49 UTC

    I can think of several reasons why you could be experiencing a segmentation fault:

    • You have use Apache in your script. That's designed for mod_perl modules; this looks more like a CGI. If you're trying to accelerate CGI with mod_perl, you need to use Apache::Registry instead. Also, if you are trying to implement a CGI, you need to use CGI and/or peek at the values in %ENV to get your input, not by accessing an Apache request object.
    • Also, if it is intended to be a content-handling mod_perl module, it should have an explicit package declaration, and be named somewhere in the Apache config with a PerlHandler directive; if you don't specify otherwise, that PerlHandler directive will be looking for a subroutine in your package called handler to process the request.
    • You have a use vars declaration in the script. Since you are invoking mod_perl, use vars is an open invitation to disaster -- those package globals you just declared are global to that Apache child process for the entirety of that child's lifetime.

    Spud Zeppelin * spud@spudzeppelin.com

      I read through your points and one thing I dont understand is why I cant use the apache request object ($r->args);

      When I read through the documentation thats what it seemed to be there for.

        I didn't want to say explicitly that you can't, because it depends on what you're trying to do. What I was saying is that from your script, I can't tell which of the following you're trying to implement:

        • A mod_perl content-handler module.
        • A CGI, accelerated with Apache::Registry.
        • Just a plain, old, run-of-the-mill CGI.

        What I was saying, if you reread my post, is that what you can and cannot access depends on which of those three you are implementing. So, which of those three are you implementing?

        Spud Zeppelin * spud@spudzeppelin.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://100795]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2024-04-19 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found