system() rc hosed?

Discussion in 'Perl Misc' started by Greg G, Oct 28, 2003.

  1. Greg G

    Greg G Guest

    I've got some code that looks like this:

    @args = ("/usr/local/bin/myprogram ", " -a ", $param1, " -b ",
    $param2);
    print STDERR @args, "\n";
    $rc = 0xffff & system (@args);

    It *should* work, but it is patently ignoring the return code from
    myprogram. In fact, the return code is 255, which seems to translate to
    "command failed". If I capture the output from stderr and run it from
    the command line, it works fine.

    In fact, this works fine:

    $capture = `@args`;
    print STDOUT $capture,"\n";

    I get the output from the myprogram.

    Any ideas what's happening to me here?

    -Greg G
    Greg G, Oct 28, 2003
    #1
    1. Advertising

  2. In article <>,
    Greg G <> wrote:
    >
    >I've got some code that looks like this:
    >
    > @args = ("/usr/local/bin/myprogram ", " -a ", $param1, " -b ",
    >$param2);
    > print STDERR @args, "\n";
    > $rc = 0xffff & system (@args);
    >
    >It *should* work, but it is patently ignoring the return code from
    >myprogram. In fact, the return code is 255, which seems to translate to
    > "command failed". If I capture the output from stderr and run it from
    >the command line, it works fine.


    When you pass system() a list of arguments, as you are doing here,
    they are taken exactly as the program to run and the arguments to
    pass to it.

    So your system() command is looking for a program called
    "/usr/local/bin/myprogram " -- without the quotes, but _with_
    the trailing space. Also, the first and third arguments to that
    program are going to have leading and trailing spaces.

    If you want to print out the arguments nicely, I would use
    print STDERR join(' ', @args), "\n";
    (You could play with the $" variable, which works out to the same thing.)

    However, I often prefer something like
    print STDERR map(">$_<", @args), "\n";
    especially if trailing spaces or newlines might be involved.

    Gary
    --
    Rule #87: If the thought of something makes me giggle for longer than 15
    seconds, I am to assume that I am not allowed to do it.
    -- www.skippyslist.com
    Gary E. Ansok, Oct 28, 2003
    #2
    1. Advertising

  3. Greg G

    Greg G Guest

    Gary E. Ansok wrote:

    > In article <>,
    > Greg G <> wrote:
    >
    >>I've got some code that looks like this:
    >>
    >> @args = ("/usr/local/bin/myprogram ", " -a ", $param1, " -b ",
    >>$param2);
    >> print STDERR @args, "\n";
    >> $rc = 0xffff & system (@args);
    >>
    >>It *should* work, but it is patently ignoring the return code from
    >>myprogram. In fact, the return code is 255, which seems to translate to
    >> "command failed". If I capture the output from stderr and run it from
    >>the command line, it works fine.

    >
    >
    > When you pass system() a list of arguments, as you are doing here,
    > they are taken exactly as the program to run and the arguments to
    > pass to it.
    >
    > So your system() command is looking for a program called
    > "/usr/local/bin/myprogram " -- without the quotes, but _with_
    > the trailing space. Also, the first and third arguments to that
    > program are going to have leading and trailing spaces.
    >
    > If you want to print out the arguments nicely, I would use
    > print STDERR join(' ', @args), "\n";
    > (You could play with the $" variable, which works out to the same thing.)
    >
    > However, I often prefer something like
    > print STDERR map(">$_<", @args), "\n";
    > especially if trailing spaces or newlines might be involved.


    Thanks! That seems to have cleared things up nicely.

    -Greg G
    Greg G, Oct 28, 2003
    #3
    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. =?Utf-8?B?UGF1bCBU?=

    SmartNavigation hosed in default web folder

    =?Utf-8?B?UGF1bCBU?=, May 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    339
    =?Utf-8?B?UGF1bCBU?=
    May 10, 2004
  2. =?Utf-8?B?amhpbGw=?=
    Replies:
    3
    Views:
    2,503
    larsinet
    Dec 29, 2004
  3. Replies:
    0
    Views:
    264
  4. Dave Sellars

    py2exe sf.net hosed???

    Dave Sellars, Nov 5, 2004, in forum: Python
    Replies:
    1
    Views:
    307
    Peter Hansen
    Nov 5, 2004
  5. Preston Crawford

    Hosed copy of Ruby, Gem, etc.?

    Preston Crawford, Jul 14, 2005, in forum: Ruby
    Replies:
    1
    Views:
    120
    Ezra Zygmuntowicz
    Jul 14, 2005
Loading...

Share This Page