Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

output of one program doesn't show in another on server

by gideondsouza (Pilgrim)
on Dec 18, 2012 at 03:03 UTC ( [id://1009269]=perlquestion: print w/replies, xml ) Need Help??

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

Forgive my naivety here I'm just trying to learn stuff and none of this is going into any production.

I have test.cgi :

use strict; use warnings; print "Content-type: text/html\n\n"; print "Test"; my @ret = `./runthis.pl`; print length(@ret); print $ret[0];

In runthis.pl I have:

use strict; use warnings; print "Hellow World";

Now, locally on my machine, if I run test.cgi as test.pl from the command line this gives @ret length as 1 and shows $ret[0] as hellow world. BUT on my apache server the array shows with a length of 1 but the output hello world doesn't show.

  1. Do I need to change the STDOUT on runthis.pl? If so how?
  2. Are there better ways to run a SAFE script as a cgi on apache and get its output into a variable? (The script is just going to output string/text)

Update:

In the end this is what worked with some trial/error suggestions from the good folk here

my @ret = qx(/usr/bin/perl ./runthis.pl`);

Replies are listed 'Best First'.
Re: output of one program doesn't show in another on server
by NetWallah (Canon) on Dec 18, 2012 at 04:39 UTC
    First, try adding the interpreter you need to run the program - depending on your OS, you may need to add:
    #!/usr/bin/perl
    at the start of your "runthis.pl".

    The second thing is that you should check the return value after running the command in backticks. Try this:

    my @ret = `\usr\bin\perl ./runthis.pl`; if ($?) { die "Error running \[runthis.pl] : $!"; }
    Getting errors back to the user's web page when running under CGI is non-trivial.
    Look at the CGI::Carp module, and the "fatalsToBrowser" option.

    Update:Fixed a few typos.

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Re: output of one program doesn't show in another on server
by AnomalousMonk (Archbishop) on Dec 18, 2012 at 04:38 UTC

    That's not the way to obtain the length (i.e., number of elements) of an array. See scalar. See also the discussion in Scalar values (in perldata) in the paragraph beginning 'The length of an array is a scalar value.' Dunno about the rest of your question.

    >perl -wMstrict -le "my @ra = qw(a b c); print length @ra; print scalar @ra; ;; @ra = (); print length @ra; print scalar @ra; " 1 3 1 0
Re: output of one program doesn't show in another on server
by mbethke (Hermit) on Dec 18, 2012 at 06:47 UTC

    What AnomalousMonk said. What happens there is that length evaluates @ret in scalar context which gives either 0 or 1 and then takes the length of this string which is 1 in both cases.

    Your real problem though is one with the working directory as Apache doesn't change it to that where the CGI lives so ./runthis.pl is not found. Try an absolute path or FindBin (ISTR the latter had some problems with CGI environments as well but it's worth a try)

      Thanks a million for your help guys

      1. I do have #!/usr/bin/perl on the top of my runthis.pl
      2. I'm running on RHEL/Apache2 on an AmazonEC2 instance.

      I updated my code according to NetWallah and AnonymousMonk's suggestions, I get the following error as predicted:

      Error running runthis.pl : No such file or directory at /var/www/html/abc.cgi line 15

      My files are in this path : /var/www/html , this is where I've configured apache to run .cgi and .pl scripts. I changed my line like this:

      my @ret = `\usr\bin\perl /var/www/html/runthis.pl`;

      No Luck, still says No such file or directory.

      1. Forgive my stupidity here, is my absolute path formed wrong?
      2. I didn't quite understand how I should use the FindBin module mbethke mentions. Any clues here would be awesome. Thanks!

         qx// `` (backticks) is double-quotish-interpolating, so  \usr\bin\perl is really:

        >perl -wMstrict -le "print qq{'\usr\bin\perl'}; " Unrecognized escape \p passed through at -e line 1. 'Sinperl'

        (Were you using warn-ings?)

        Update: Oops... Fixed example code to include initial backslash. (And always, always avoid Sinperl!)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found