How do I distinguish a file from a directory in Perl?

后端 未结 6 1117
天涯浪人
天涯浪人 2020-12-31 01:54

I\'m trying to traverse through all the subdirectories of the current directory in Perl, and get data from those files. I\'m using grep to get a list of all files and folder

6条回答
  •  有刺的猬
    2020-12-31 02:07

    Look at the -X operators:

    perldoc -f -X
    

    For directory traversal, use File::Find, or, if you're not a masochist, use my File::Next module which makes an iterator for you and doesn't require crazy callbacks. In fact, you can have File::Next ONLY return files, and ignore directories.

    use File::Next;
    
    my $iterator = File::Next::files( '/tmp' );
    
    while ( defined ( my $file = $iterator->() ) ) {
        print $file, "\n";
    }
    
    # Prints...
    /tmp/foo.txt
    /tmp/bar.pl
    /tmp/baz/1
    /tmp/baz/2.txt
    /tmp/baz/wango/tango/purple.txt
    

    It's at http://metacpan.org/pod/File::Next

提交回复
热议问题