Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Array for system() call

by MZSanford (Curate)
on Oct 23, 2001 at 16:53 UTC ( #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.


Comment on Re: Array for system() call
Select or Download Code
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.

    dmm

    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 ./arrayslice.pl 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)

      -Blake

        Thanks for the example, blakem

        dmm

        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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2015-07-02 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (34 votes), past polls