Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

how to store values from system command

by veeruch (Sexton)
on Nov 21, 2006 at 17:52 UTC ( [id://585314]=perlquestion: print w/replies, xml ) Need Help??

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

hi perlmonks,
if i give a code like given below ,i couldn't get the $val how can i get it the values;
/usr/bin/perl -w use strict; my $val = system("ls -lrt "); print "$val";

Replies are listed 'Best First'.
Re: how to store values from system command
by ikegami (Patriarch) on Nov 21, 2006 at 17:54 UTC
Re: how to store values from system command
by andyford (Curate) on Nov 21, 2006 at 18:13 UTC
    Please ignore this post if you really really want the output of 'ls',
    but if you're doing what I think you're doing, take a step back and check out the documention for the internal Perl ways of doing things.
    This will allow you to build a robust, portable, and maintainable application.

    reading directories

    getting the properties of files

    non-Perl: Andy Ford

Re: how to store values from system command
by swampyankee (Parson) on Nov 21, 2006 at 18:59 UTC

    In your sample, $val will contain the return code from ls. Most system commands return 0 or undef on success, so your system("ls -lrt "); call is succeeding (I'm quite sure ls can fail; I just can't remember seeing it do so.).

    As ikegami and andyford mentioned, using system is almost certainly not the best solution. For one thing, it uses a sub-shell, where using glob or opendir doesn't.

    You could try something like this (see stat):

    #!perl use strict; use warnings; my @list = glob("*"); foreach(@list){ (my $dev, my $ino, my $mode, my $nlink, my $uid, my $gid, my $rdev, my $size, my $atime, my $mtime, my $ctime, my $blksize, my $blocks) = stat; # do processing here }

    yeah, verily, an update

    ikegami pointed out that system only uses a subshell if the command contains shell metacharacters. In any case, unless you need the data exactly as produced by ls, using the Perl functions is certainly more portable (Windows doesn't have ls), and possibly faster.


    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.


    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

      $val will contain the return code from ls.

      [system] uses a sub-shell

      One of those statments is necessarily false. If a shell is used, $val will contain the return value of the shell, not ls.

      So does system use a shell? It depends. "If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing." Otherwise, no.

      In this case? no. However, I still wouldn't use ls.

      Some examples:

      system('command foobar') # no shell system('command "foo bar"') # shell system('command', 'foo bar') # no shell system('sort < file | uniq') # shell

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://585314]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found