glob('foo *')相对于的比较优势是什么?

时间:2022-09-01 22:51:56

I was just looking at Can someone tell me how to create an array of directory contents?. Unsurprisingly, file globs were offered as an answer. What surprised me was that the post recommended using glob() rather than <*>.

我只是在看有人可以告诉我如何创建一个目录内容数组?不出所料,提供文件全球作为答案。让我感到惊讶的是帖子建议使用glob()而不是<*>。

I use <*> because that is what I learned a long time ago and have never thought about it. Which is not a good reason.

我使用<*>因为这是我很久以前学到的,从未想过它。这不是一个好理由。

What syntax do you prefer for globbing, and why?

您喜欢使用什么语法进行通配,为什么?

3 个解决方案

#1


12  

Personally, I think that <> is badly overloaded. It sort of means "read from an iterator" but there's a little too much magic and subtlety in determining which iterator of what kind for my taste. e.g. <$foo> and <$foo{bar}> mean very different things, and the magical bare <> means something different yet.

就个人而言,我认为<>严重超载。它有点意思是“从迭代器中读取”,但在确定哪种迭代器符合我的口味时,有一点太多的魔力和微妙之处。例如<$ foo>和<$ foo {bar}>意味着非常不同的东西,而神奇的裸<>意味着不同的东西。

I prefer to use glob and reserve the <> syntax for handles. Doing that saves the cognitive burden of figuring out whether the contents of the <> is a pattern rather than a handle (or something that resolves to one). That said, if you mentally associate the <> notation with "iterator" instead of "read" you might prefer that to the functional syntax -- at least for those cases where you want to step over the results rather than generate a list.

我更喜欢使用glob并为句柄保留<>语法。这样做可以节省认知负担,即确定<>的内容是模式而不是句柄(或解析为一个)。也就是说,如果您在心理上将<>表示法与“迭代器”而不是“读取”相关联,那么您可能更喜欢使用功能语法 - 至少对于那些想要跳过结果而不是生成列表的情况。

#2


10  

That perldoc you linked says there's some issues with the angle-bracket syntax:

您链接的perldoc表示角括号语法存在一些问题:

One level of double-quote interpretation is done first, but you can't say <$foo> because that's an indirect filehandle as explained in the previous paragraph. (In older versions of Perl, programmers would insert curly brackets to force interpretation as a filename glob: <${foo}> . These days, it's considered cleaner to call the internal function directly as glob($foo), which is probably the right way to have done it in the first place.)

首先进行一级双引号解释,但你不能说<$ foo>,因为这是一个间接的文件句柄,如前一段所述。 (在旧版本的Perl中,程序员会插入大括号来强制解释为文件名glob:<$ {foo}>。现在,将内部函数直接调用为glob($ foo)被认为更干净,这可能是正确的方法是首先完成它。)

I mostly use File::Find and its ilk myself.

我自己主要使用File :: Find等等。

#3


2  

These days I stick with glob, because I find it to be more readable and newbie-friendly(*) as compared to using angle brackets.

这些天我坚持使用glob,因为与使用尖括号相比,我发现它更具可读性和新手友好性(*)。

(*) perldoc -f glob works.

(*)perldoc -f glob工作。

#1


12  

Personally, I think that <> is badly overloaded. It sort of means "read from an iterator" but there's a little too much magic and subtlety in determining which iterator of what kind for my taste. e.g. <$foo> and <$foo{bar}> mean very different things, and the magical bare <> means something different yet.

就个人而言,我认为<>严重超载。它有点意思是“从迭代器中读取”,但在确定哪种迭代器符合我的口味时,有一点太多的魔力和微妙之处。例如<$ foo>和<$ foo {bar}>意味着非常不同的东西,而神奇的裸<>意味着不同的东西。

I prefer to use glob and reserve the <> syntax for handles. Doing that saves the cognitive burden of figuring out whether the contents of the <> is a pattern rather than a handle (or something that resolves to one). That said, if you mentally associate the <> notation with "iterator" instead of "read" you might prefer that to the functional syntax -- at least for those cases where you want to step over the results rather than generate a list.

我更喜欢使用glob并为句柄保留<>语法。这样做可以节省认知负担,即确定<>的内容是模式而不是句柄(或解析为一个)。也就是说,如果您在心理上将<>表示法与“迭代器”而不是“读取”相关联,那么您可能更喜欢使用功能语法 - 至少对于那些想要跳过结果而不是生成列表的情况。

#2


10  

That perldoc you linked says there's some issues with the angle-bracket syntax:

您链接的perldoc表示角括号语法存在一些问题:

One level of double-quote interpretation is done first, but you can't say <$foo> because that's an indirect filehandle as explained in the previous paragraph. (In older versions of Perl, programmers would insert curly brackets to force interpretation as a filename glob: <${foo}> . These days, it's considered cleaner to call the internal function directly as glob($foo), which is probably the right way to have done it in the first place.)

首先进行一级双引号解释,但你不能说<$ foo>,因为这是一个间接的文件句柄,如前一段所述。 (在旧版本的Perl中,程序员会插入大括号来强制解释为文件名glob:<$ {foo}>。现在,将内部函数直接调用为glob($ foo)被认为更干净,这可能是正确的方法是首先完成它。)

I mostly use File::Find and its ilk myself.

我自己主要使用File :: Find等等。

#3


2  

These days I stick with glob, because I find it to be more readable and newbie-friendly(*) as compared to using angle brackets.

这些天我坚持使用glob,因为与使用尖括号相比,我发现它更具可读性和新手友好性(*)。

(*) perldoc -f glob works.

(*)perldoc -f glob工作。