Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Need help with Apache2::Subprocess undefined value error when using spawn_proc_prog

by aels (Initiate)
on Aug 16, 2007 at 02:14 UTC ( #632901=perlquestion: print w/replies, xml ) Need Help??
aels has asked for the wisdom of the Perl Monks concerning the following question:

I need to test the use of Apache2::SubProcess to execute an external command from a web page.
To create a small test case I use the following:
#!/usr/bin/perl -w use strict; use CGI qw/:standard/; use Apache2::SubProcess (); my $out; my $r = shift; my $command = "/bin/echo 123 > /tmp/test"; print header; print "<p>"; print "test"; print "</p>"; $out = $r->spawn_proc_prog($command); print end_html;
But when I execute this, I get:
Can't call method "spawn_proc_prog" on an undefined value at /srv/www/cgi-bin/ line 15

Any ideas what I am doing wrong.
Thank you in advance.
  • Comment on Need help with Apache2::Subprocess undefined value error when using spawn_proc_prog
  • Download Code

Replies are listed 'Best First'.
Re: Need help with Apache2::Subprocess undefined value error when using spawn_proc_prog
by GrandFather (Sage) on Aug 16, 2007 at 02:23 UTC

    Consider the two lines:

    my $r = shift; ... $out = $r->spawn_proc_prog($command);

    and think about what $r actually can contain, and what it needs to contain to be dereferenced (see perlref). Keep in mind that shift is acting on @ARGV here.

    DWIM is Perl's answer to Gödel

      so $r is undefined (empty) in the above case.
      and need to be the current handler?
        Is this code intended to be run under ModPerl::Registry?
Re: Need help with Apache2::Subprocess undefined value error when using spawn_proc_prog
by Errto (Vicar) on Aug 16, 2007 at 19:21 UTC
    Assuming you are running under Apache::Registry? Then the correct way to obtain the request object is my $r = Apache->request and not my $r = shift.
      No, this should work. The Apache::RequestRec object is supposed to be passed to the sub generated by ModPerl::Registry.

        I reinstalled with Apache2.2.4 and mod_perl 2.0.3 and I am getting the code to work now. I will post an update later tonight with a working example.

      The code need to run under mod_perl 2 and apache 2.2.
      The specified web page will have connections to Oracle and PostgreSQL to get information, then using a form you will select information and the submit will execute an os executable to perform certain tasks depending on selection. A similar web page use to run perfect under apache 1.3 and mod_perl 1 using "system($command)" to run the os command, but when doing this via the later versions the web browser hangs, but the command is executed fine. The command is executed but the web pages is waiting for it. The command executes in under 3 seconds. After doing some research a came accross the Apache2::SubProcess, which should allow me to execute the command and the web page should be able to then function as normal and not hang. After the command is executed the page will display the command output (reading a file and printing it to screen).

      The versions of software we use is:
      Apache/2.2.0 (Unix) mod_perl/2.0.2 Perl/v5.8.5, running on RHES 4.
        That all makes sense. Nonetheless I am fairly sure my earlier reply addresses the initial question you raised about that undefined reference error.
        I don't see any reason why system() should not work under mod_perl 2. I also think your use of $r in this code should work. Can you verify that this code is running through mod_perl by checking $ENV{MOD_PERL} within the script?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://632901]
Approved by chargrill
[holli]: meh, silent night
[holli]: also sad
[shmem]: halluzination beats newspaper in realism, sometimes

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-09-22 20:57 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (269 votes). Check out past polls.