Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Reading

by mr.nick (Chaplain)
on Feb 05, 2001 at 20:47 UTC ( #56427=note: print w/replies, xml ) Need Help??


in reply to Reading from ls-lrt (was: Reading)

Instead of spawning an "ls" process, why not use only Perl stuff?:
#!/usr/bin/perl sub getlast5 { my $dir=shift; my @files; opendir DIR,$dir || die "Couldn't open '$dir': $!"; for my $x (sort { (stat($b))[9] <=> (stat($a))[9] } readdir DIR) { next if $x eq "." || $x eq ".."; push @files,$x; last if $#files==4; } closedir DIR; @files; } print join("\n",getlast5(".")),"\n";
(it's pretty simple code, but I'm trying different styles :)

Replies are listed 'Best First'.
(ichimunki) suggestion regarding Perl-ized 'ls -lrt'
by ichimunki (Priest) on Feb 05, 2001 at 20:53 UTC
    You may want to move the readdir DIR step to a point just prior to the sort so that you can properly filter the directory there... perhaps using a glob or File::Find. This way we can explicitly include only the file types we want, rather than excluding the . and .. (what about .bash_history or such files?) inside the for loop.
      I presume that files beginning with a dot where still eligable for transfer as he didn't say anything about that. But if he's mirroring an FTP site, I suppose a
      next if $x=~/^\./;
      would be appropriate (and take care of the directory stuff).
      You be right, ichimunki, by using the example of 'ls -ltr', it doesn't include dot files ;)
        Actually, he is using 'ls -lrt'. If I'm not mistaken this will not display .files in the list, so we need that. I'd rather not include any invalid files in my sort process to begin with, hence my suggestion. In fact, if we start out with the correct file list and sort it based on stat, then we just need to do @return_list = @sort_list[0..4] without looping through the list at all.
Re: Re: Reading
by mkmcconn (Chaplain) on Feb 06, 2001 at 05:11 UTC
    opendir DIR,$dir || die "Couldn't open '$dir': $!";

    For the main point of your code, I think it's excellent the way that you've used sort() on stat() .
      But, I hope it's helpful to point out that the opendir() statement is broken, as it was written at the time of my response, because it tests $dir for Truth rather than opendir() , and that's not what you want.

    To use the perlop '||' with opendir() the parentheses are not optional. Otherwise, use or which is more reliable in statements such as open() and opendir() , because the precedence of or is so low that it doesn't even require those pesky parentheses.

    (deleted my own bad -here)

    But, as I said, this is a small tweak that shouldn't take away from what seems to me otherwise to be a good idea.


    mkmcconn

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2023-09-25 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?