chdir "/etc" or die " $!"; #change the directory to /etc/
- foreach (@ARGV){
-
print "one arg is $_\n";
-
}
perl glob.pl *.pl will list all files end with .pl.
foreach (glob "*.pl *.txt"){
- print "one arg is $_\n";
-
}
this will list all files end with .pl and .txt.
An Alternate Syntax for Globbing <> angle-bracket syntax
my @all_files = <*>; ## exactly the same as my @all_files = glob "*";
So, if using angle brackets means both filehandle reading and globbing, how does Perl
decide which of the two operators to use? Well, a filehandle has to be a Perl identifier.
So, if the item between the angle brackets is strictly a Perl identifier, it’s a filehandle
read; otherwise, it’s a globbing operation. For example:
- my @files = <FRED/*>; ## a glob
-
my @lines = <FRED>; ## a filehandle read
-
my $name = "FRED";
-
my @files = <$name/*>; ## a glob
readdir reads the content under the directory.
- my $dirname = "/home/yliu/test/";
-
opendir DIR, $dirname or die "$!\n";
-
foreach (readdir DIR){
- next if (/^\./);
-
print "$_\n";
-
}
-
closedir DIR;
Like filehandles, directory handles are automatically closed at the end of the program
or if the directory handle is reopened onto another directory.
chdir函数能够更改当前的工作目录。
它不接受任何参数时,能将目录更改到用户的主目录中。如果调用成功,该函数将返回1,否则就返回0。系统错误代码将保存在Perl的$!变量中 。
阅读(450) | 评论(0) | 转发(0) |