Chinaunix首页 | 论坛 | 博客
  • 博客访问: 839466
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-14 10:59:47

chdir "/etc" or die " $!"; #change the directory to /etc/
  1. foreach (@ARGV){
  2.     print "one arg is $_\n";
  3. }
perl glob.pl *.pl will list all files end with .pl.

foreach (glob "*.pl *.txt"){
  1. print "one arg is $_\n";
  2. }
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:
  1. my @files = <FRED/*>; ## a glob
  2. my @lines = <FRED>; ## a filehandle read
  3. my $name = "FRED";
  4. my @files = <$name/*>; ## a glob
readdir reads the content under the directory.
  1. my $dirname = "/home/yliu/test/";
  2. opendir DIR, $dirname or die "$!\n";
  3. foreach (readdir DIR){
  4.     next if (/^\./);
  5.     print "$_\n";
  6. }
  7. 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的$!变量中 。








阅读(421) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~