@ARGV array incorrect when calling perl program from system

Discussion in 'Perl Misc' started by Udo Grabowski, Sep 3, 2009.

  1. Hi,

    the following construction gives additional @ARGV entries which shouldn't
    be there:

    @command = ("perl-program","--help",">","logfile");
    eval { system(@command) };

    Inside perl-program (which starts with #!/usr/bin/perl),
    @ARGV now contains also '>' and 'logfile' as additional
    entries. This contradicts the definition of @ARGV:
    "The array @ARGV contains the command-line arguments
    INTENDED FOR THE SCRIPT"

    Since the redirection parameters are not script parameters, they
    shouldn't appear (and didn't appear before). This bug breaks all
    and everything depending on @ARGV and the number of parameters !

    Is this a perl bug, or is the system command of the OS responsible
    for this broken behaviour ? Or even the distributor of this perl
    version ?

    Platform: OpenSolaris 2009.06,
    Version: perl 5.8.8 (Coolstack distribution)
    i86pc-solaris-thread-multi
    Udo Grabowski, Sep 3, 2009
    #1
    1. Advertising

  2. Udo Grabowski <> writes:

    > @command = ("perl-program","--help",">","logfile");
    > eval { system(@command) };


    Read the documentation for the system command:

    If there is more than one argument in LIST, or if LIST is an array
    with more than one value, starts the program given by the first
    element of the list with arguments given by the rest of the list.

    So of course '>' and 'logfile' is parsed to the script as script
    arguments. If you need any kind of shell handling, you have to use
    one, and only one, argument to system.

    I can't imagine that this has changed recently on any posix like
    platform.

    //Makholm
    Peter Makholm, Sep 3, 2009
    #2
    1. Advertising

  3. Peter Makholm wrote:
    > Udo Grabowski <> writes:
    >
    >> @command = ("perl-program","--help",">","logfile");
    >> eval { system(@command) };

    >
    > Read the documentation for the system command:
    >
    > If there is more than one argument in LIST, or if LIST is an array
    > with more than one value, starts the program given by the first
    > element of the list with arguments given by the rest of the list.
    >
    > So of course '>' and 'logfile' is parsed to the script as script
    > arguments. If you need any kind of shell handling, you have to use
    > one, and only one, argument to system.



    So this means that calling 'perl-program --help > logfile' and
    using the construction above (which should be equivalent, since
    it should be transparent to the program from whom it is called)
    will give inconsistent @ARGV arrays. So I still consider this
    to be a bug (referring to Unix semantics) instead of a feature.

    If this is really Posix' definition, then Posix is inconsistent.
    But I think that 'system' called here is the perl implementation,
    so maybe that is simply done wrong there.

    Workaround is indeed to call it this way:
    $return=system('perl-program --help > logfile')
    Udo Grabowski, Sep 3, 2009
    #3
  4. Udo Grabowski wrote:
    > Peter Makholm wrote:
    >> Udo Grabowski <> writes:
    >>
    >>> @command = ("perl-program","--help",">","logfile");
    >>> eval { system(@command) };

    >>
    >> Read the documentation for the system command:
    >>
    >> If there is more than one argument in LIST, or if LIST is an array
    >> with more than one value, starts the program given by the first
    >> element of the list with arguments given by the rest of the list.
    >> So of course '>' and 'logfile' is parsed to the script as script
    >> arguments. If you need any kind of shell handling, you have to use
    >> one, and only one, argument to system.

    >
    >
    > So this means that calling 'perl-program --help > logfile' and
    > using the construction above (which should be equivalent,


    No they should not be equivalent.

    perldoc -f system

    "Note that argument processing varies
    depending on the number of arguments. If there is more than one
    argument in LIST, or if LIST is an array with more than one
    value, starts the program given by the first element of the list
    with arguments given by the rest of the list. If there is only
    one scalar argument, the argument is checked for shell
    metacharacters, and if there are any, the entire argument is
    passed to the system's command shell for parsing"

    In one case a shell is *not* ever invoked, and therefore shell features
    such as output redirection are not applicable.

    In the other case, a shell *will* be invoked to process any shell
    metacharacters.

    > since
    > it should be transparent to the program from whom it is called)
    > will give inconsistent @ARGV arrays. So I still consider this
    > to be a bug (referring to Unix semantics) instead of a feature.


    If a program's behaviour agrees with it's documentation I consider it a
    feature. No matter how much I might dislike or be confused by that feature.

    Since changing this feature would break many existing Perl programs I
    strongly doubt it will ever get changed in Perl 5.

    --
    RGB
    RedGrittyBrick, Sep 3, 2009
    #4
  5. Lawrence Statton wrote:
    > One builds the argv[] array manually, and
    > forks and execs your binary without ever going near a shell.


    Thanks for pointing that out, it wasn't clear to me from the doc
    that no shell is involved when calling it this way. Maybe
    your sentence would be helpful in the doc to avoid confusion.
    Udo Grabowski, Sep 3, 2009
    #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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,560
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,925
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    887
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    653
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page