- #!/usr/bin/perl -w
-
#
-
use strict;
-
-
my $dir = opendir(DIR, "/home/yliu/test/") or die "Cannot open the dir\n";
-
my @files = readdir(DIR);
-
-
foreach my $file (@files) {
-
print $file, "\n";
-
}
-
close(DIR);
-
-
my @files_1 = </home/yliu/test/*>;
-
foreach my $file_1 (@files_1){
-
printf("$file_1\n");
-
}
-
-
my @files_2 = glob '/home/yliu/perl_scripts/*.pl';
-
-
foreach my $file_2 (@files_2){
-
printf("$file_2\n");
-
}
-
~
glob EXPR
In list context,returns a list of filenames expansions on the value of EXPR. In scalar context, glob iterates through such filename expansions, return undef if the list is exhausted. This is the internal function implementing the <*.c> operator.
Note that glob splits its arguments on whitespace and treats each segment as seperate pattern. As such, glob("*.c *.h") matches all the file with .c or .h extensions.The expression glob("*.c *") matches all the files in the current working directory.
If non-empty braces are the only wildcard characters used in the
, no filenames are matched, but potentially many strings
are returned. For example, this produces nine strings, one for
each pairing of fruits and colors:
- my @many = glob "{apple,tomato,cherry}={green,yellow,red}";
-
foreach my $a (@many){
-
print $a ."\n";
-
}
-
-
apple=green
-
apple=yellow
-
apple=red
-
tomato=green
-
tomato=yellow
-
tomato=red
-
cherry=green
-
cherry=yellow
-
cherry=red
还可以用 File::Find列出子文件夹里的内容。
阅读(1695) | 评论(0) | 转发(0) |