perl中,chdir函数用来改变当前的工作目录到指定目录,如果没有指定目录,则到用户的主目录。这个函数的调用是相当于Unix中的cd命令来切换目录。
下面通过一个具体的例子来看一下chdir是如何工作的。
perl
use strict;
use warnings;
# Print all files in a directory
sub print_files {
my $dir = 'd:/code';
opendir DIR, $dir or die $!;
my @files = readdir DIR;
chdir $dir; # Use chdir or -f will not work, since -f need absolutely path
foreach my $file (@files) {
if (-f $file) {
print "$file\n";
}
}
closedir DIR;
}
&print_files();
评论
0评论加载中…