glob('foo*') 相对于的比较优势是什么?
我只是在看 可以有人告诉我如何创建目录内容数组?。不出所料,文件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言,我认为
<>
严重过载。它有点意味着“从迭代器读取”,但是在确定哪种迭代器适合我的口味时有点太多的魔力和微妙之处。例如<$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.您链接的那个 perldoc 说尖括号语法存在一些问题:
我主要使用
File::Find
我自己也是如此。That perldoc you linked says there's some issues with the angle-bracket syntax:
I mostly use
File::Find
and its ilk myself.这些天我坚持使用
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.