system() behavior

Discussion in 'Perl Misc' started by skochkarev@gmail.com, Oct 24, 2008.

  1. Guest

    Hi,

    I'm new to perl and find it strange sometimes. So, my question is.

    As stated to perldoc, system() does not output. This is true for my
    console application. But when I port it to Apache/cgi, it starts to
    display all output. This is highly unwanted. Can I turn it off?

    Another question. The `` works just I want with one difference: it
    seems like perl does not stop until the `` finishes (but the rest of
    my program needs `` finished). What operator should I put to make perl
    wait until `` finishes?

    Here is the piece of code:

    -----------------------------------------------------------------------------------
    my $cmdline = "c:\\tools\\decomp.bat";
    my $param1 = "$tempdir";
    my $param2 = "C:$tempfile";

    my @args = ("$cmdline", "\"$param1\"", "\"$param2\"");
    ------ This operator puts all output to the browser ------------
    system(@args);

    ------ This operator does not stop processing ------------
    my $output = `$cmdline $param1 $param2`;

    ------- Code that reads files created by decomp.bat --------

    Thanks in advance!
    , Oct 24, 2008
    #1
    1. Advertising

  2. <> wrote:
    > Hi,
    >
    > I'm new to perl and find it strange sometimes. So, my question is.
    >
    > As stated to perldoc, system() does not output.


    Where does it say that?

    system() doesn't output itself, nor does it capture output. But the
    program that system() runs may output.

    > This is true for my
    > console application. But when I port it to Apache/cgi, it starts to
    > display all output. This is highly unwanted. Can I turn it off?


    Have whatever system() is running not output. Maybe you want to
    redirect the output to a file or to /dev/null....

    > Another question. The `` works just I want with one difference: it
    > seems like perl does not stop until the `` finishes (but the rest of
    > my program needs `` finished). What operator should I put to make perl
    > wait until `` finishes?


    I don't know what youe mean with that sentence.

    >
    > Here is the piece of code:



    > ------ This operator puts all output to the browser ------------
    > system(@args);
    >
    > ------ This operator does not stop processing ------------
    > my $output = `$cmdline $param1 $param2`;


    Both system and `` will wait until the command completes before
    returning.

    --
    Darren
    Darren Dunham, Oct 24, 2008
    #2
    1. Advertising

  3. Guest

    "" <> wrote:
    > Hi,
    >
    > I'm new to perl and find it strange sometimes. So, my question is.
    >
    > As stated to perldoc, system() does not output.


    It says that system does not *capture* output. Instead, the output
    leaks out to the started program's stdout, which is usually the same thing
    as the parent's STDOUT.

    > This is true for my
    > console application. But when I port it to Apache/cgi, it starts to
    > display all output. This is highly unwanted. Can I turn it off?


    redirect stdout to /dev/null, or whatever Windows equiv of that is.

    >
    > Another question. The `` works just I want with one difference: it
    > seems like perl does not stop until the `` finishes


    I don't know what that means. Perl doesn't restart until `` finishes,
    it just sits in the `` operation and wait for it finish.

    > (but the rest of
    > my program needs `` finished). What operator should I put to make perl
    > wait until `` finishes?


    That is already what it does.


    >
    > ------ This operator does not stop processing ------------
    > my $output = `$cmdline $param1 $param2`;


    What does that mean? What are you seeing?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Oct 24, 2008
    #3
  4. "" <> wrote:
    >As stated to perldoc, system() does not output.


    That is not true. Or maybe it is, depending upon what you mean by "does
    not output".
    - the system() command does not _produce_ any output. If you want that
    use print()
    - it also doesn't capture or influence the output generated by the
    command that is called. If the called command produces output, then it
    will be displayed just as if system() was never involved.

    >This is true for my
    >console application. But when I port it to Apache/cgi, it starts to
    >display all output. This is highly unwanted. Can I turn it off?


    If you script behaves differently from a command line then when run as a
    CGI program, then you may want to investigate why. A good place to start
    is some CGI newsgroup.
    As for turning output off: well, system() doesn't produce any output in
    the first place. You will have to silence whatever external command you
    are running, maybe by using some option of that command if available or
    by redirecting that commands output to a file or /dev/null or something
    along that line.

    >Another question. The `` works just I want with one difference: it
    >seems like perl does not stop until the `` finishes (but the rest of
    >my program needs `` finished).


    Ahhhmmm, no. system() and qx() (aka ``) will both stop execution of the
    perl program until the external command has returned.
    Maybe you are running the external command in the background, such that
    the calling shell returns immediately?

    >What operator should I put to make perl
    >wait until `` finishes?


    `` does wait for the external program to return. Your problem is with
    the external program. Maybe it launches child processes in the
    background?

    >Here is the piece of code:
    >
    >-----------------------------------------------------------------------------------
    >my $cmdline = "c:\\tools\\decomp.bat";
    >my $param1 = "$tempdir";
    >my $param2 = "C:$tempfile";
    >
    >my @args = ("$cmdline", "\"$param1\"", "\"$param2\"");
    >------ This operator puts all output to the browser ------------
    >system(@args);


    No, it doesn't. system() happily ignores any output from the called
    program. It's that external program that creates the output.

    jue
    Jürgen Exner, Oct 24, 2008
    #4
    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. Me
    Replies:
    2
    Views:
    466
  2. Gene
    Replies:
    0
    Views:
    570
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,733
    Dan Pop
    Feb 17, 2004
  4. Ben Floyd
    Replies:
    1
    Views:
    313
    Donn Cave
    Jan 15, 2004
  5. Replies:
    1
    Views:
    760
    Martin v. Löwis
    Jan 24, 2008
Loading...

Share This Page