Problem printing array content with CGI

Discussion in 'Perl Misc' started by Simon L, Sep 24, 2004.

  1. Simon L

    Simon L Guest

    hi,

    I have a script that calls another module
    to exec a script, gets the output back in a array,
    and prints out the content of the array using CGI.
    If I run the script at the command line, it prints
    everything from the array correctly. However, if
    I run it on the brower, the program runs but nothing
    from the array gets displayed on the page. I have
    struggled with it for a while, any insight would be
    greatly appreciated.

    my $line;
    my (@stdout, @stderr);

    # $program_name is another Perl script
    my $scr = '/user/ice/htdocs/' . $program_name;

    # command to be sent to another program to exec

    my @cmd = ($scr, '-mode', $Mode, '-U', 'xxx', '-P', 'xxx', '-App', $AppName);

    # send for exec and gets its STDOUT and STDERR back using
    # @stdout and @stderr

    my $rc = ForkAndReadStdoutStderr(\@stdout, \@stderr, @cmd);
    if ($rc < 0)
    { print $Cgi->p($ICE::Fork::ERROR) }
    if ($rc > 0)
    { print $Cgi->p("@cmd FAILED! stdout=@stdout;stderr=@stderr") }

    print header('text/plain');

    foreach $line (@stdout)
    {
    print $line;
    }

    My temporary solution now is to send the output
    of the script that I system() to a file and
    use "print redirect()" to display the file.

    Thanks,
    SL
    Simon L, Sep 24, 2004
    #1
    1. Advertising

  2. Simon L

    Shawn Corey Guest

    Hi,

    If a CGI works from a command line but not when called by a web server
    then 95% of the time it's a problem with permissions. Check _all_ files
    including the data files.

    --- Shawn

    Simon L wrote:
    > hi,
    >
    > I have a script that calls another module
    > to exec a script, gets the output back in a array,
    > and prints out the content of the array using CGI.
    > If I run the script at the command line, it prints
    > everything from the array correctly. However, if
    > I run it on the brower, the program runs but nothing
    > from the array gets displayed on the page. I have
    > struggled with it for a while, any insight would be
    > greatly appreciated.
    >
    > my $line;
    > my (@stdout, @stderr);
    >
    > # $program_name is another Perl script
    > my $scr = '/user/ice/htdocs/' . $program_name;
    >
    > # command to be sent to another program to exec
    >
    > my @cmd = ($scr, '-mode', $Mode, '-U', 'xxx', '-P', 'xxx', '-App', $AppName);
    >
    > # send for exec and gets its STDOUT and STDERR back using
    > # @stdout and @stderr
    >
    > my $rc = ForkAndReadStdoutStderr(\@stdout, \@stderr, @cmd);
    > if ($rc < 0)
    > { print $Cgi->p($ICE::Fork::ERROR) }
    > if ($rc > 0)
    > { print $Cgi->p("@cmd FAILED! stdout=@stdout;stderr=@stderr") }
    >
    > print header('text/plain');
    >
    > foreach $line (@stdout)
    > {
    > print $line;
    > }
    >
    > My temporary solution now is to send the output
    > of the script that I system() to a file and
    > use "print redirect()" to display the file.
    >
    > Thanks,
    > SL
    Shawn Corey, Sep 24, 2004
    #2
    1. Advertising

  3. Shawn Corey wrote:
    > If a CGI works from a command line but not when called by a web
    > server then 95% of the time it's a problem with permissions.


    Relative paths is a rather common cause as well.

    > Check _all_ files including the data files.


    And ensure that they are called with full paths.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 24, 2004
    #3
  4. Simon L

    Simon L Guest

    Gunnar Hjalmarsson <> wrote in message news:<>...
    > Shawn Corey wrote:
    > > If a CGI works from a command line but not when called by a web
    > > server then 95% of the time it's a problem with permissions.

    >
    > Relative paths is a rather common cause as well.
    >
    > > Check _all_ files including the data files.

    >
    > And ensure that they are called with full paths.


    Thanks for your responses. I used absolute path. I subsequently
    wrote the code below, this time, it involves no files but only
    a pipe declared before a fork, and still, it works at command line
    but display nothing on the browser. I am starting to think
    maybe I missed something very basic. Any idea?

    pipe(FROM_CHILD, TO_PARENT);

    $pid = fork;
    my $data=undef;

    if($pid) { # parent
    close(TO_PARENT);
    $data = <FROM_CHILD>;
    print header('text/plain');
    print $data;
    my $id = wait();
    print "id=$id\n";
    } else { #child
    close(FROM_CHILD);
    print TO_PARENT "Hello, call from child\n";
    exit(0);
    }

    Thanks,
    SL
    Simon L, Sep 25, 2004
    #4
  5. Simon L

    Simon L Guest

    Steve May <> wrote in message news:<>...
    > Simon L wrote:
    >
    > > Gunnar Hjalmarsson <> wrote in message news:<>...
    > >
    > >>Shawn Corey wrote:
    > >>
    > >>>If a CGI works from a command line but not when called by a web
    > >>>server then 95% of the time it's a problem with permissions.
    > >>
    > >>Relative paths is a rather common cause as well.
    > >>
    > >>
    > >>>Check _all_ files including the data files.
    > >>
    > >>And ensure that they are called with full paths.

    > >
    > >
    > > Thanks for your responses. I used absolute path. I subsequently
    > > wrote the code below, this time, it involves no files but only
    > > a pipe declared before a fork, and still, it works at command line
    > > but display nothing on the browser. I am starting to think
    > > maybe I missed something very basic. Any idea?

    >
    >
    > This really sounds more like a server issue.
    >
    > I believe I'd be looking at my error log for clues....
    >
    >
    > \s


    Thank you all for your responses. It was indeed a server issue. We did
    not compile Perl/mod_perl with sfio for security reason and hence
    system(), exec, pipe calls will not send output to the browser. I switched
    to use backticks to launch Perl scripts and it seems to work fine.

    Cheers,
    SL
    Simon L, Sep 30, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hazz
    Replies:
    6
    Views:
    49,528
    SkyUCHC
    Jun 9, 2010
  2. Luigi Ma
    Replies:
    0
    Views:
    131
    Luigi Ma
    Nov 21, 2007
  3. Victor Reyes
    Replies:
    2
    Views:
    97
    Victor Reyes
    Apr 7, 2008
  4. John Bokma
    Replies:
    10
    Views:
    1,002
    Tad McClellan
    May 11, 2004
  5. BestFriend
    Replies:
    2
    Views:
    753
Loading...

Share This Page