免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2247 | 回复: 8
打印 上一主题 下一主题

glob in scalar context [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-16 17:32 |只看该作者 |倒序浏览
本帖最后由 atiking 于 2010-12-16 17:33 编辑

起因是我看了这篇
http://coding.derkeiler.com/Archive/Perl/comp.lang.perl.misc/2007-06/msg01421.html

于是我做了些修改
  1. ~/temp $ touch foo.1 foo.2 foo.3
  2. ~/temp $ perl -le'
  3. for (1..10) {
  4. if (glob("foo.$_")) {
  5. print "yes"
  6. } else {
  7. print "no"
  8. }
  9. }
  10. '
  11. yes
  12. no
  13. yes
  14. no
  15. yes
  16. no
  17. yes
  18. no
  19. yes
  20. no
复制代码
为什么呢,以我的理解结果应该是

  1. yes
  2. yes
  3. yes
  4. no
  5. no
  6. no
  7. no
  8. no
  9. no
  10. no
复制代码

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2010-12-16 18:19 |只看该作者
起因是我看了这篇


于是我做了些修改为什么呢,以我的理解结果应该是
atiking 发表于 2010-12-16 17:32



if (glob("foo.$_")) {
改   
if ( -f "foo.$_") { 或 if ( -e "foo.$_") {

论坛徽章:
0
3 [报告]
发表于 2010-12-16 18:48 |只看该作者
if (glob("foo.$_")) {
改   
if ( -f "foo.$_") { 或 if ( -e "foo.$_") {
jason680 发表于 2010-12-16 18:19


这么改没错

只是想知道glob为什么会有那样的结果

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2010-12-16 20:41 |只看该作者
按文档中说 scalar context 中 glob 返回个 iterator,最后会返回个 undef 表示结束,教你怎么查检
  1. for (1..10) {
  2. if ($name = glob("foo.$_")) {
  3. print "yes ", $name;
  4. } else {
  5. print "no"
  6. }
  7. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-12-17 09:27 |只看该作者
起因是我看了这篇


于是我做了些修改为什么呢,以我的理解结果应该是
atiking 发表于 2010-12-16 17:32


    From "perldoc -f glob":

    In scalar context, glob iterates through ... filename expansions,
    returning undef when the list is exhausted.

So glob() is returning the file pattern itself on the first call, as
Charles described, and then undef on the second call to indicate that
the list is complete. The cycle continues on the third call and so on.

To avoid this effect, call glob() in list context, like this perhaps:

  for (1..10) {
    if (my @x = glob "foo.$_") {
      print "@x\n";
      print "yes";
    }
    else {
      print "@x\n";
      print "no";
    }
    print "-------------";
  }

HTH,

Rob

论坛徽章:
0
6 [报告]
发表于 2010-12-17 09:28 |只看该作者
按文档中说 scalar context 中 glob 返回个 iterator,最后会返回个 undef 表示结束,教你怎么查检
zhlong8 发表于 2010-12-16 20:41


那这个iterator是根据什么返回的呢
现在来看似乎和glob中的pattern没什么关系?

论坛徽章:
0
7 [报告]
发表于 2010-12-17 09:29 |只看该作者
那这个iterator是根据什么返回的呢
现在来看似乎和glob中的pattern没什么关系?
atiking 发表于 2010-12-17 09:28


    There weren't any  matches with files and no wildcards
in the glob pattern so the pattern itself is returned *.  If
there had been a wildcard in the pattern, then nothing
would have been returned.

perl -E "say glob('foo.3')"   <--- returns foo.3 *
perl -E "say glob('foo?3)"   <--- doesn't return anything

   *  You'd have to use bsd_glob in order to  not return
       the pattern if no files match, eg:
              use File::Glob ':glob';
              say bsd_glob('foo.3',GLOB_ERR);

      Otherwise, if there had been a file "foo.3" with an
      actual '.' in the filename, glob('foo.3') would also
      return that filename.

Perl uses File::Glob for globbing since 5.6.1 and  only
these metacharacters are recognized:

      \       Quote the next metacharacter
      []      Character class
      {}      Multiple pattern
      *       Match any string of characters
      ?       Match any single character
      ~       User name home directory

For more detail: perldoc File::Glob

Perhaps, you were thinking '.' was also a glob wildcard
as it  would be in a regex pattern...

--
Charles DeRykus

论坛徽章:
0
8 [报告]
发表于 2010-12-17 09:38 |只看该作者
回复 5# 兰花仙子
也就是说在scalar的环境下,如果在list没有结束的情况下
再次glob中的pattern无效
glob会继续返回上次pattern的list中的下一个结果

论坛徽章:
0
9 [报告]
发表于 2010-12-17 10:59 |只看该作者
回复  兰花仙子
也就是说在scalar的环境下,如果在list没有结束的情况下
再次glob中的pattern无效
glob ...
atiking 发表于 2010-12-17 09:38


我又升级了一下测试脚本,验证了上述的推论

  1. ~/temp $ touch foo.1 foo.11 foo.2 foo.3 foo.4 foo.5 foo.6
  2. ~/temp $ cat test.pl
  3. #/usr/bin/perl
  4. use strict;

  5. for (1..10) {
  6.     print "$_\t";
  7.    
  8.     if (my file glob("foo.*$_")) {
  9.         print "$file\tyes"
  10.     } else {
  11.         print "no"
  12.     }
  13.     print "\n";
  14. }
  15. ~/temp $ perl test.pl

  16. 1    foo.1    yes
  17. 2    foo.11  yes
  18. 3    no
  19. 4    foo.4    yes
  20. 5    no
  21. 6    foo.6    yes
  22. 7    no
  23. 8    no
  24. 9    no
  25. 10  no
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP