11

Is there any guarantee that the array of filenames returned from a glob (e.g. <*>) will be sorted?

I can't find that sorting is mentioned one way or the other in the documentation, but it seems to be the case in every directory I've tried it on.

I'm talking about using this syntax:

@files = <*>;

If I need the files to be sorted, would the below be redundant?

@files = sort(<*>);
4

1 回答 1

24

在 Perl 5.6.0 和更新版本中,文件名是排序的:

从 v5.6.0 开始,此运算符使用标准 File::Glob 扩展名实现。

--用于 glob 的 perldoc

默认情况下,路径名按 ASCII 升序排序。

--用于 File::Glob 的 perldoc

有一个问题:

默认情况下,假定文件名区分大小写

--用于 File::Glob 的 perldoc

说了这么多,您可以更改此行为以不区分大小写地排序

use File::Glob qw(:globally :nocase);

请注意, :globally 自 5.6.0 以来是多余的,但这也适用于旧版本。

或者,如果您只想做一个不区分大小写的全局:

use File::Glob ':glob';

@files = bsd_glob('*', GLOB_NOCASE);
于 2009-09-22T18:06:23.963 回答