在Perl中,有什么理由更喜欢glob而不是readdir(反之亦然)? [英] What reasons are there to prefer glob over readdir (or vice-versa) in Perl?

查看:150
本文介绍了在Perl中,有什么理由更喜欢glob而不是readdir(反之亦然)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是一个.一段历史:当我第一次学习Perl时,我几乎总是使用glob而不是opendir + readdir,因为我发现它更容易.然后,后来的各种文章和读物表明glob不好,所以现在我几乎总是使用readdir.

This question is a spin-off from this one. Some history: when I first learned Perl, I pretty much always used glob rather than opendir + readdir because I found it easier. Then later various posts and readings suggested that glob was bad, and so now I pretty much always use readdir.

考虑了最近的问题我意识到,我选择其中一项的原因可能是仓位.因此,我将阐述一些优点和缺点,并希望更多有经验的Perl人士可以对此进行说明.简而言之,是(在某些情况下还是在某些情况下)是否有令人信服的理由倾向于使用glob而不是readdirreaddir而不是glob?

After thinking over this recent question I realized that my reasons for one or the other choice may be bunk. So, I'm going to lay out some pros and cons, and I'm hoping that more experienced Perl folks can chime in and clarify. The question in a nutshell is are there compelling reasons to prefer glob to readdir or readdir to glob (in some or all cases)?

  1. 没有点文件(除非您要它们)
  2. 保证订单顺序
  3. 无需在目录上手动添加目录名称
  4. 更好的名字(如果仅按名字来判断,那就来吧-globreaddir毫无关系)
  5. (根据ysth的回答;请参见下面的glob缺点4)可以返回不存在的文件名:

  1. No dotfiles (unless you ask for them)
  2. Order of items is guaranteed
  3. No need to prepend the directory name onto items manually
  4. Better name (c'mon - glob versus readdir is no contest if we're judging by names alone)
  5. (From ysth's answer; cf. glob cons 4 below) Can return non-existent filenames:

@deck = glob "{A,K,Q,J,10,9,8,7,6,5,4,3,2}{\x{2660},\x{2665},\x{2666},\x{2663}}";

glob缺点:

glob cons:

  1. 较早的版本只是断掉了(但我认为'older'意味着5.6之前的版本,坦率地说,如果您使用的是5.6之前的Perl,则会有更大的问题)
  2. 每次
  3. 呼叫stat(即在大多数情况下无用的stat).
  4. 目录名称中包含空格的问题(这仍然是真的吗?)
  5. (来自brian的答案)可以返回不存在的文件名:

  1. Older versions are just plain broken (but 'older' means pre 5.6, I think, and frankly if you're using pre 5.6 Perl, you have bigger problems)
  2. Calls stat each time (i.e., useless use of stat in most cases).
  3. Problems with spaces in directory names (is this still true?)
  4. (From brian's answer) Can return filenames that don't exist:

$ perl -le 'print glob "{ab}{cd}"'

readdir优点:

readdir pros:

  1. (来自brian的回答)opendir返回一个文件句柄,您可以在程序中传递该文件句柄(并重复使用),但是glob只是返回一个列表
  2. (来自brian的回答)readdir是一个适当的迭代器,并为rewinddirseekdirtelldir
  3. 提供功能
  4. 更快? (基于上面glob的某些功能进行的纯猜测.无论如何,我并不真正担心这种优化级别,但这是理论上的优点.)
  5. glob容易出现边缘错误吗?
  6. 默认情况下读取所有内容(也包括点文件)(这也是一个缺点)
  7. 可以说服您不要命名文件0(也有一个弊端-请参见Brad的答案)
  8. 有人吗?布勒? Bueller?
  1. (From brian's answer) opendir returns a filehandle which you can pass around in your program (and reuse), but glob simply returns a list
  2. (From brian's answer) readdir is a proper iterator and provides functions to rewinddir, seekdir, telldir
  3. Faster? (Pure guess based on some of glob's features from above. I'm not really worried about this level of optimization anyhow, but it's a theoretical pro.)
  4. Less prone to edge-case bugs than glob?
  5. Reads everything (dotfiles too) by default (this is also a con)
  6. May convince you not to name a file 0 (a con also - see Brad's answer)
  7. Anyone? Bueller? Bueller?

readdir缺点:

readdir cons:

  1. 如果您不记得在目录名前加上,当您尝试进行文件测试或复制项目或编辑项目或...时,您会会得到的帮助.
  2. 如果您不记得要grep..项目,则 会在您计数项目时得到一些帮助,或者尝试递归地沿着文件树或...
  3. 我有没有提到目录名的前面? (附带说明,但我在Perl Beginners邮件列表中的第一篇文章就是经典的为什么在某些时候涉及文件测试的代码为什么不起作用?"与此陷阱有关的问题.显然,我还是很苦.)/li>
  4. 项目以不特定的顺序返回.这意味着您将经常需要记住以某种方式对它们进行排序. (如果这意味着更快的速度,并且这意味着您实际上考虑关于如何以及是否需要对项目进行排序,那么这可能是一个专家.)编辑:样本太少了,但在Mac readdir上,按字母顺序返回项目,不区分大小写.在Debian盒子和OpenBSD服务器上,顺序是完全随机的.我使用苹果内置的Perl(5.8.8)和我自己编译的5.10.1测试了Mac. Debian框为5.10.0,OpenBSD机器也是如此.我想知道这是否是文件系统问题,而不是Perl?
  5. 默认情况下(这也是专业人士)读取所有内容(也包括点文件)
  6. 不一定要很好地处理名为0的文件(另请参阅专家-请参见Brad的答案)
  1. If you don't remember to prepend the directory name, you will get bit when you try to do filetests or copy items or edit items or...
  2. If you don't remember to grep out the . and .. items, you will get bit when you count items, or try to walk recursively down the file tree or...
  3. Did I mention prepending the directory name? (A sidenote, but my very first post to the Perl Beginners mail list was the classic, "Why does this code involving filetests not work some of the time?" problem related to this gotcha. Apparently, I'm still bitter.)
  4. Items are returned in no particular order. This means you will often have to remember to sort them in some manner. (This could be a pro if it means more speed, and if it means that you actually think about how and if you need to sort items.) Edit: Horrifically small sample, but on a Mac readdir returns items in alphabetical order, case insensitive. On a Debian box and an OpenBSD server, the order is utterly random. I tested the Mac with Apple's built-in Perl (5.8.8) and my own compiled 5.10.1. The Debian box is 5.10.0, as is the OpenBSD machine. I wonder if this is a filesystem issue, rather than Perl?
  5. Reads everything (dotfiles too) by default (this is also a pro)
  6. Doesn't necessarily deal well with a file named 0 (see pros also - see Brad's answer)

推荐答案

您错过了它们之间最重要,最大的区别:glob为您提供了一个列表,但opendir为您提供了目录句柄.您可以传递该目录句柄,以让其他对象或子例程使用它.有了目录句柄,子例程或对象就不必知道它来自何处,还有谁在使用它,等等:

You missed the most important, biggest difference between them: glob gives you back a list, but opendir gives you a directory handle. You can pass that directory handle around to let other objects or subroutines use it. With the directory handle, the subroutine or object doesn't have to know anything about where it came from, who else is using it, and so on:

 sub use_any_dir_handle {
      my( $dh ) = @_;
      rewinddir $dh;
      ...do some filtering...
      return \@files;
      }

使用副手柄,您可以控制迭代器,使用seekdir可以在其中移动,尽管使用glob您只能获得下一个项目.

With the dirhandle, you have a controllable iterator where you can move around with seekdir, although with glob you just get the next item.

尽管如此,成本和收益只有在应用于特定环境时才有意义.它们在特定用途之外不存在.您可以很好地列出它们之间的差异,但是在不知道您要如何处理它们的情况下,我不会对这些差异进行分类.

As with anything though, the costs and benefits only make sense when applied to a certain context. They do not exist outside of a particular use. You have an excellent list of their differences, but I wouldn't classify those differences without knowing what you were trying to do with them.

其他一些要记住的东西:

Some other things to remember:

  • 您可以使用opendir来实现自己的glob,但不能反过来.

  • You can implement your own glob with opendir, but not the other way around.

glob使用其自己的通配符语法,仅此而已.

glob uses its own wildcard syntax, and that's all you get.

glob可以返回不存在的文件名:

glob can return filenames that don't exist:

$ perl -le 'print glob "{ab}{cd}"'

这篇关于在Perl中,有什么理由更喜欢glob而不是readdir(反之亦然)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆