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

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-09-02 14:04:00

  1. #!/usr/bin/perl -w
  2. #
  3. use strict;

  4. my $dir = opendir(DIR, "/home/yliu/test/") or die "Cannot open the dir\n";
  5. my @files = readdir(DIR);

  6. foreach my $file (@files) {
  7.     print $file, "\n";
  8. }
  9. close(DIR);

  10. my @files_1 = </home/yliu/test/*>;
  11. foreach my $file_1 (@files_1){
  12.     printf("$file_1\n");
  13.     }

  14. my @files_2 = glob '/home/yliu/perl_scripts/*.pl';

  15. foreach my $file_2 (@files_2){
  16.     printf("$file_2\n");
  17.     }
  18. ~
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:
  1. my @many = glob "{apple,tomato,cherry}={green,yellow,red}";
  2. foreach my $a (@many){
  3.     print $a ."\n";
  4.     }

  5. apple=green
  6. apple=yellow
  7. apple=red
  8. tomato=green
  9. tomato=yellow
  10. tomato=red
  11. cherry=green
  12. cherry=yellow
  13. cherry=red
还可以用 File::Find列出子文件夹里的内容。






阅读(1695) | 评论(0) | 转发(0) |
0

上一篇:q, qq, qx, q

下一篇:文件操作

给主人留下些什么吧!~~