Why doesn't Perl file glob() work outside of a loop in scalar context?

后端 未结 4 1186
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 15:49

According to the Perl documentation on file globbing, the <*> operator or glob() function, when used in a scalar context, should iterate through the list of files matching

4条回答
  •  半阙折子戏
    2021-02-14 15:53

    The following code also seems to create 2 separate instances of the iterator...

    for ( 1..3 )
    {
       $filename = <*>;
       print "$filename\n" if defined $filename;
       $filename = <*>;
       print "$filename\n" if defined $filename;
    }
    

    I guess I see the logic there, but it is kind of counter intuitive and contradictory to the documentation. The docs don't mention anything about having to be in a loop for the iteration to work.

提交回复
热议问题