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

hi i am working in a linux environment , and suppose i am writing a program that runs a inbuild system command like
while($file=<*>) { system(`cat $file;`); print "\n"; }
i am not geting the output.Can anyone of u insist me what to do or add to this program ?

2005-02-26 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: how to run system commands
by holli (Abbot) on Feb 24, 2005 at 11:36 UTC
    Your are using backticks (``) in combination with the system-command. that is plain wrong.
    You will have to use print `command`; or system ("command");

    Clarification: backticks execute the command and return the output (see perlop). system() will execute the command, while any output of the command will go directly to sdtout.


    holli, /regexed monk/
Re: how to run system commands
by mr_jpeg (Beadle) on Feb 24, 2005 at 11:52 UTC
    hm.
    The first thing that springs to mind is that you don't need the system call here, you only need the backticks, e.g.,

    my $text = `cat $file`;

    Do you know about perldoc? It's a great tool. It documents every single function in perl; it's a real lifesaver. Type

    perldoc -f system
    at a prompt and you'll get all the info you need about system, and how to get the output of a command.

    Just a suggestion:
    I'd probably do something like the following:

    #!/usr/bin/perl -w use strict; my $file; my @files = glob("bin/*.pl"); foreach $file (@files) { open(FILE, $file) || die "Couldn't open $file: $!\n"; while(<FILE>) { chomp $_; print("$_\n"); } close(FILE); }
    Use perldoc -f to look up each of the glob, open, die, while, chomp, and close functions.
Re: how to run system commands
by sh1tn (Priest) on Feb 24, 2005 at 11:51 UTC
    You may want to see the following items:
    perldoc -f system
    perldoc -f exec
    perldoc -q command


Re: how to run system commands
by samizdat (Vicar) on Feb 24, 2005 at 15:21 UTC
    First, don't combine system() and backticks, as has been explained.
    #!/usr/bin/perl -w $file = 'index.php'; system("cat $file") && die "System call failure: $!\n"; print "\n";
    Second, always assume the system call will fail at some time and add an error printout using die or Carp. UN!X System calls use the opposite return result convention to regular perl calls like open(), that's why && rather than the more-usual or.

    Update: Also, you can do this without spawning an extra shell by separating the words of the system call:
    system('cat', "file") && ..
Re: how to run system commands
by perlfan (Parson) on Feb 24, 2005 at 15:56 UTC
    You can use the qx// operator as well.
Re: how to run system commands
by m-rau (Scribe) on Feb 25, 2005 at 00:19 UTC
    Besides all the good advices, this print `command`; might confuse you if you have buffered output. Check out $| = 1 to see the command's output straight away and read i.e. Screen Output Buffering.
A reply falls below the community's threshold of quality. You may see it by logging in.