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

发布于 2024-08-18 03:01:44 字数 515 浏览 7 评论 0原文

我只是在看 可以有人告诉我如何创建目录内容数组?。不出所料,文件 glob 被提供作为答案。令我惊讶的是,该帖子推荐使用 glob() 而不是 <*>

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

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

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 <*>.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆离笙 2024-08-25 03:01:44

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

我更喜欢使用 glob 并为句柄保留 <> 语法。这样做可以减轻弄清楚 <> 的内容是否是模式而不是句柄(或解析为句柄的东西)的认知负担。也就是说,如果您在心里将 <> 表示法与“迭代器”而不是“读取”联系起来,那么您可能更喜欢它而不是函数语法——至少对于那些您想要跳过的情况结果而不是生成列表。

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.

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.

栀子花开つ 2024-08-25 03:01:44

您链接的那个 perldoc 说尖括号语法存在一些问题:

一级双引号
解释是首先完成的,但是你
不能说 <$foo> 因为那是
间接文件句柄,如中所述
上一段。 (在较旧的
Perl 版本,程序员会
插入大括号以强制
解释为文件名 glob:
<${foo}> 。如今,人们认为
更清洁地调用内部函数
直接作为 glob($foo) ,即
可能是正确的做法
首先。)

我主要使用 File::Find 我自己也是如此。

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

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.
)

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

两仪 2024-08-25 03:01:44

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

(*) perldoc -f glob 有效。

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

(*) perldoc -f glob works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文