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.

In reply to Re: how to store values from system command by swampyankee
in thread how to store values from system command by veeruch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.