G Nagasri Varma has asked for the wisdom of the Perl Monks concerning the following question:

1. system command : ls | awk -F'[_]' '{print $2}' o/p :
1431024666 1431037440 1431038327
2. Perl program
#!/usr/bin/perl @ab=`ls | awk -F'[_]' '{print $2}'`; print "\@ab = @ab";
output :
@ab = ab_1431024666_1430960820_36398 ab_1431037440_1430958175_36390 ab_1431038327_1430958130_36387
Question : I want to get the same output of the system command when i run the perl script. Can someone, guide what is missing here?
  • Comment on O/p of sysem command should be assigned to array variable in perl program and display the output.
  • Select or Download Code

Replies are listed 'Best First'.
Re: O/p of sysem command should be assigned to array variable in perl program and display the output.
by Corion (Patriarch) on Dec 02, 2015 at 14:18 UTC

    Backticks interpolate, so `... $2 ...` will likely not do what you think it does. If you allowed Perl to help you, it would tell you about this problem. This is why a common suggestion is to add use warnings; to the top of your scripts.

    Even better would be to eliminate the use of ls and awk in favour of readdir and stat.

Re: O/p of sysem command should be assigned to array variable in perl program and display the output.
by toolic (Bishop) on Dec 02, 2015 at 14:20 UTC