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

Hi all, I'm seeking enlightenment on a question that I cannot find a solution to anywhere. I'm new to Perl programming, but use C and Java. Please look at a sample line of code and tell me where I'm going wrong. This code does display the output from file in the terminal, but I want to capture this output to a scalar for further processing.
$type = system 'file', "/home/patt/DCUProject/cli.pl";

The output it produces is:
/home/patt/DCUProject/cli.pl: ASCII English text

It is this I want to capture, especially the file characteristics - ASCII English text. Thanking you in advance...

Replies are listed 'Best First'.
Re: Capturing command outputs
by moritz (Cardinal) on May 10, 2009 at 11:35 UTC
      Many thanks moritz. I am getting what I wanted now. I was trying qx, but in this fashion:
      system qx(file /home/patt/DCUProject/cli.pl);
      It never dawned on me to exclude 'system' from the line of code. I had tried backticks, but they won't work for some reason.
Re: Capturing command outputs
by jethro (Monsignor) on May 10, 2009 at 11:42 UTC
    Capturing command output can be done with backticks $type= `file /home/patt/DCUProject/cli.pl`. Another possibility is to open a pipe, see 'perldoc -f open'
Re: Capturing command outputs
by Perlbotics (Archbishop) on May 10, 2009 at 19:36 UTC

    There's also an XS module available which might be a useful alternative: File::LibMagic

    Example (shameless theft from the docs):

    use File::LibMagic ':easy'; print MagicBuffer("Hello World\n"),"\n"; # returns "ASCII text" print MagicFile("/bin/ls"),"\n"; # returns "ELF 32-bit LSB executable, Intel 80386, version 1 +(SYSV)" # on my system
    HTH