note
haukex
<p>I looked into this further and was able to reproduce the issue using your code, and find an explanation and workaround. [doc://glob] in scalar context acts like an iterator. However, the iterator state is attached to the [doc://glob] call site, <i>even</i> when the argument to [doc://glob] changes. Here's a simple way to reproduce the issue:</p>
<c>
$ touch foo bar
$ perl -MData::Dump -e '
sub myglob {scalar glob($_[0])}
dd myglob($_) for qw/foo bar/ '
"foo"
undef
</c>
<p>One might expect the output here to be <c>"foo"</c> and <c>"bar"</c>. There is an excellent discussion in [https://rt.perl.org/Public/Bug/Display.html?id=123404|RT#123404], and it doesn't sound like this behavior is going to change, as potentially confusing as it is.</p>
<p>A simple workaround is to force list context:</p>
<c>
return if ()=glob("$directory/*");
</c>
1187638
1187638