Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Fixed-length buffered input fr ...Fetch a record from a file (Berikutnya)
Input and output functions

Get a directory from a directory handle

Daftar Isi

  • readdir DIRHANDLE

    Returns the next directory entry for a directory opened by opendir.If used in list context, returns all the rest of the entries in thedirectory. If there are no more entries, returns the undefined value inscalar context and the empty list in list context.

    If you're planning to filetest the return values out of a readdir, you'dbetter prepend the directory in question. Otherwise, because we didn'tchdir there, it would have been testing the wrong file.

    1. opendir(my $dh, $some_dir) || die "can't opendir $some_dir: $!";
    2. @dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);
    3. closedir $dh;

    As of Perl 5.11.2 you can use a bare readdir in a while loop,which will set $_ on every iteration.

    1. opendir(my $dh, $some_dir) || die;
    2. while(readdir $dh) {
    3. print "$some_dir/$_\n";
    4. }
    5. closedir $dh;

    To avoid confusing would-be users of your code who are running earlierversions of Perl with mysterious failures, put this sort of thing at thetop of your file to signal that your code will work only on Perls of arecent vintage:

    1. use 5.012; # so readdir assigns to $_ in a lone while test
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Fixed-length buffered input fr ...Fetch a record from a file (Berikutnya)