perl - system command

Can this be done without using te system command? I have a directory with a large number of files in it, but I am interested in only the 8 most recent.

The directory looks like
-rw-rw-rw- 1 adsm adsm 13412 Sep 22 08:31 events_dump_09222005.csv.gz
-rw-rw-rw- 1 adsm adsm 13405 Sep 21 08:31 events_dump_09212005.csv.gz
-rw-rw-rw- 1 adsm adsm 13237 Sep 20 08:31

I am getting the date out of the file to be used in another module. What I have works, but want I want to know is if it can be done without resorting to temporary file (listfiles) and the system command

#!/usr/bin/perl

system( 'ls -tr /adsm/DATASRC/events/ | tail -8r > /adsm/SCRIPTS/BKUPINFO/listfiles');
open (LSTFILES, "</adsm/SCRIPTS/BKUPINFO/listfiles");
while (<LSTFILES>) {
print($);
($pt1,$pt2) = split(/_dump
/, $_ );
chomp($pt2);
print "$pt2\n";
($pt3,$pt4) = split(/\./,$pt2);
print "$pt3\n";
}

close(LSTFILES);

Yes, in fact I won't resort to shell equivalents if something can be solely done from within Perl, because this ensures the highest portability. The following will give you a list of top 8 files with the latest mtime in descending order of mtime:

#!/usr/bin/perl

my $base = "/adsm/DATASRC/events/";

opendir DIR, $base or die "Cannot read dir!";
my @list = readdir DIR;
closedir DIR;
my @list_with_mtime = map { ($_ !~ /^\./?[$_, (stat($base . $_))[9]]:()) } @list;
my @r_sorted_list = map { $$_[0] } sort { $$b[1] <=> $$a[1] } @list_with_mtime;

# Show top 8
{
	local $, = "\n";
	print @r_sorted_list[0..7];
}

Thanks for the reply. It absolutely does what I am looking for.
Now I need to understand it. My understanding of the map function is that it works like a foreach loop. Is that right?

Can you explain this statement map { ($_ !~ /^\./?[$, (stat($base . $))[9]]:()) } @list

It looks complex, but it is actually quite simple if I put it in a nicer looking form.

map {
     (     
           $_ !~ /^\./?
                 [
                       $_, 
                       (stat($base . $_))[9]
                 ]:
                 ()
    ) 
} @list

So we read the list of file (including directory entries) names in @list. For each name read, we carry out the following operation:

  • If the filename does not start with a ".", it is not a hidden file. We will put the filename together with its mtime as an array reference to facilitate sorting later on.
  • Otherwise, a hidden file is ignored by yielding an empty array.

It will pass through every entry of @list and collect the evaluated value in the map{} block and return the aggregated list. At the end of this statement we will get an array of array references containing the filename and the corresponding mtime. Entries that yield an empty array are ignored because an empty array upon interpolated in a list will "dissolve" itself. The net effect in this piece of code is that hidden files will disappear from the resulting, aggregated list. This is what we want if we want to ignore all hidden files.

i.e. ("a", "b", (), "c") will give ("a", "b", "c") so this is how this trick works.

Yes, the next statement sorts this intermediate list in descending order of mtime, and then reconstruct an array with filenames only (by cleaning the temporary mtime values and removing the references).

Thank you for the reply.

I am also trying to understand the sort statement { $$_[0] } sort { $$b[1] <=> $$a[1] . I understant what it does but what I am not familiar with is the $$.

As I said, I have made some temporary references with the first statement. In my case this is an array reference (think about it like a pointer to an array) containing the filename together with the mtime as two separate array items.

To access an item from a normal array by index you will

$array[0], $array[1]

If the corresponding array is pointed to by a reference and you need to access an item through the reference, that becomes

$$arrayref[0], $$arrayref[1]

OR (both syntax will work)

$arrayref->[0], $arrayref->[1]

which literally means "dereference and then index in the underlying array".

Understanding these require some knowledge about references in Perl.

The official page that talks about this:
http://perldoc.perl.org/perlref.html