On Sun, Jul 06, 2003 at 02:43:14PM +0530 Pandey Rajeev-A19514 wrote:

> Do we have a perl function equivalent with the same functionality as
> the function "scandir" in c language ?

No, we don't. Simply do it yourself:

    my $dir = "/path";
    opendir DIR, $dir or die $!;
    my @list = sort { $b cmp $a }
               grep { -f "$dir/$_" }
               readdir DIR;

    closedir DIR;

This spits out all regular files in /path in reverse order.

If you want a function, then wrap the above into something like this
untested snippet:

    sub scandir {
        my ($dir, $sel, $sort);
        local *DIR;
        opendir DIR, $dir or die $!;
        return sort { $sort->($a, $b) }
               grep { $sel->("$dir/$_") }
               readdir DIR;
    }

    print scandir("/path", sub { -f $_[0] }, sub { $_[1] cmp $_[0] });

Just as scandir(3), this one takes references to the select and sort
function that are used to filter and order the output accordingly. If
you write

    return sort { $sort->() }
           ...

(and even if not), you can also use

    sub { $b <=> $a }

as a reference to the sorting-routine. But using @_ for both of them is
more consistent.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to