in reply to Interacting with the shell ( backticks, system(), open -| )

What is the advantage of using open FP, "find . -ls |" and then looping through the handle, as opposed to using backticks?
  1. If the output is extensive, reading and processing it a line at a time rather than slurping it all into memory then processing it, may avoid using a large amount of memory.
  2. If the output takes a while to produce, your program can be processing the first lines whilst the other process is still producing the rest.
  3. Maybe you don't need all the output.

    Once you found what you are looking for, you can discard the rest.

    If you capture the pid returned by the piped open, you can even kill the external process before it completes.

regarding system(), it returns the exact same thing as using backticks

What makes you think this? system does not capture or return any output from the program.

why did the coder use a pipe in open() instead of using backticks ...?

Perhaps he was dealing with very large directories.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: Interacting with the shell ( backticks, system(), open -| )

Replies are listed 'Best First'.
Re^2: Interacting with the shell ( backticks, system(), open -| )
by jktstance (Novice) on Feb 05, 2014 at 15:00 UTC
    Thank you. So piping the command to a filehandle should keep the find . -ls process open until <FP> is closed? Anyway, it makes more sense to pipe if it doesn't slurp everything up at once like assigned the command to an array.

    I also responded to davido's reply above as to why system() is returning the output of the command, in addition to the exit status.

      So piping the command to a filehandle should keep the find . -ls process open until <FP> is closed?

      The external process is connected to your end of the pipe via one or two buffers, usually 4k or 8k each.

      When (if) those buffers fill up, the external process will be blocked when trying to add more output to the pipe and thus will have to wait until your program reads some of the data from your end of the pipe before continuing.

      If the external program produces less output than the combined size of the buffers, it will run to completion immediately and the output will wait in the pipe until your program reads it.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.