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

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

by gideondsouza (Pilgrim)
on Dec 18, 2012 at 03:03 UTC ( #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 = `./`; print length(@ret); print $ret[0];

In I have:

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

Now, locally on my machine, if I run test.cgi as 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 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)


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

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

Comment on output of one program doesn't show in another on server
Select or Download Code
Re: output of one program doesn't show in another on server
by AnomalousMonk (Monsignor) 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 NetWallah (Abbot) 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:
    at the start of your "".

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

    my @ret = `\usr\bin\perl ./`; if ($?) { die "Error running \[] : $!"; }
    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 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 ./ 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
      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 : 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/`;

      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?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-08-30 07:26 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (291 votes), past polls