What are the comparative advantages of glob('foo*') over <foo*>?

半城伤御伤魂 提交于 2019-12-09 14:27:39

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2087981/what-are-the-comparative-advantages-of-globfoo-over-foo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!