Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an simple question. My perl program invokes another program called getfilesdata using system() and needs to catch output from getfilesdata to use in the perl program. getfilesdata takes as input the filename and prints some information to screen. Issue: I need to get the output from the getfilesdata into my perl program.
system(getfilesdata file.txt); //calls getfilesdata Example of output from getfilesdata: An 32 bit string and file name is + displayed on the screen. kdw3343kajkdaa11dkf file.txt

Replies are listed 'Best First'.
Re: Question using system.
by toolic (Bishop) on Jul 10, 2008 at 14:53 UTC

      Or you may use open with pipe, as described in perlipc. That allows you to work with the commands output as with any other (readonly) filehandle.

      open(my $cmd, 'getfilesdata file.txt |')
        Better yet:
        open(my $cmd, '-|', 'getfilesdata', 'file.txt')
      Why can't I get the output of a command with system()?
      Because it is not designed that way. system has a different purpose, that is running a program, which even might be interactive. Think about, for instance, system('bash').
      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Question using system.
by harishnuti (Beadle) on Jul 10, 2008 at 14:51 UTC

    one simple solution can be as below...
    my $output = undef; $output = qx(getfilesdata file.txt); # which is same as using back ticks $output = `getfilesdata file.txt`;

    you will get output in variable, but remember you cannot check return code
      but remember you cannot check return code
      From perlvar, $?:
      The status returned by the last pipe close, backtick (`` ) command, successful call to wait() or waitpid(), or from the system() operator.
      use strict; use warnings; my $out; $out = `ls /tmp`; print "tmp status: $?\n"; $out = `ls /foo`; print "foo status: $?\n"; __END__ tmp status: 0 ls: /foo: No such file or directory foo status: 256