Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Array for system() call

by MZSanford (Curate)
on Oct 23, 2001 at 16:53 UTC ( [id://120770]=note: print w/replies, xml ) Need Help??

in reply to Array for system() call

When system() gets a scalar, it goes to the shell to execute the command. This means that you open a new shell instance (such a ksh), and run the program, including any redirection.

If system() gets an array, it instead does the fork() and then uses exec() to start the process. This by-passes the shell, so shell redirection does not work.
i had a memory leak once, and it ruined my favorite shirt.

Replies are listed 'Best First'.
Re (2): Array for system() call
by dmmiller2k (Chaplain) on Oct 23, 2001 at 22:09 UTC

    To elaborate further, use this (note double quotes): system("@Call"); instead of this: system(@Call); to achieve the same effect as your first fragment.

    And, BTW, you probably meant to do this:

    $Call[0] = "gawk"; $Call[1] = "-f"; $Call[2] = "test.gawk"; $Call[3] = "test.file"; $Call[4] = ">"; $Call[5] = "out.file";

    or (slightly more efficiently) this:

    @Call = ( "gawk", "-f", "test.gawk", "test.file", ">", "out.file" );

    rather than your original code, which is assigning values to 1-element array slices, which are sublists -- NOT arrays! -- of references to the elements of the @Call array; needless to say, although it works, it is somewhat less efficient than either of the above approaches, and bad practice besides.


    Give a man a fish and you feed him for a day ...
    Teach the man to fish and you feed him for a lifetime
      Here is an example of why using @arr[0] is just asking from trouble...
      #!/usr/bin/perl -wT use strict; my @arr; @arr[0] = localtime(); $arr[1] = localtime(); print "\$arr[0] = $arr[0]\n"; print "\$arr[1] = $arr[1]\n";
      Any guesses about the output of this simple script? Select area below to find out.

      =OUTPUT Scalar value @arr[0] better written as $arr[0] at ./ line + 6. $arr[0] = 58 $arr[1] = Tue Oct 23 14:30:58 2001
      5 points for anyone who expected $arr[0] to contain an integer. 20 points for anyone who knew it would contain the seconds value, *not* the number of elements in the list returned by localtime().

      (10 points for anyone who coded up an example because they weren't sure... woohoo)


        Thanks for the example, blakem


        You can give a man a fish and feed him for a day ...
        Or, you can teach him to fish and feed him for a lifetime

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://120770]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-05-21 01:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found