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

I don't see your code where a call to system produces the same output as backticks. Can you provide an example that we can execute ourselves to see what you mean?

The equivalence of `find . -ls`; and open FP, 'find . -ls |' is kind of where the functional overlap ends. Backticks are more confining than using open, and while you can emulate what backticks do using open, you cannot emulate everything that open can do using backticks. And with backticks you get everything back at the same time. In the simple example you presented, one of the best reasons to go with the open version is to allow you to work on the output one line at a time, without storing it all in an array. And the advantage of the backticks in this same example is just the simplicity and brevity.


Dave

Replies are listed 'Best First'.
Re^2: Interacting with the shell ( backticks, system(), open -| )
by jktstance (Novice) on Feb 05, 2014 at 14:57 UTC
    Dave, Here is the script I use when using system():
    #! /usr/bin/perl use strict; use warnings; my @files = system "find . -ls"; foreach my $file (@files) { chomp $file; print "$file\n"; }
    And it outputs the following (I deleted the userid column):
    1105134059 2 drwxr-xr-x 3 .... users 2048 Feb 5 09:40 . 1105135378 1 -rw-r--r-- 1 .... users 506 Feb 3 10:54 ./t +est.pl 1105135090 1 -rw-r--r-- 1 .... users 93 Jan 24 15:41 ./c +alendar.pl 0
    Notice the trailing 0? If I replace my @files = system "find . -ls"; with my @files = `find . -ls`;, I get the same output, except the trailing 0 is missing.

    I understand why system() prints out the trailing 0 (it's the exit status of find()), but why is it also outputting everything find() outputs? I thought it should only return the exit status.

      find itself outputs the lines. System does not return them, it only returns the exit status. You can verify none of the lines is printed via print by wrapping the values in parentheses:
      print "($file)\n";

      The zero will be the only parenthesized string.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Have you compared system against qx? They do different things. Most importantly, system does not return the output of the child.