I found this piece of wisdom in the PerlFaq quoted in a chat board from 2000.
Is there a leak/bug in glob()?
Due to the current implementation on some operating systems, when you use the glob() function or its angle-bracket alias in a scalar context, you may cause a leak and/or unpredictable behavior. It's best therefore to use glob() only in list context.
I read that this problem was fixed in Perl 5.6 but later heard a report that it still occurs in 5.10.1
Has anyone had any experience of recent problems and where would be the best place to find the definitive answer regarding this?
[Later..] The latest PerlFAQ says:
5.18: Is there a leak/bug in glob()?
(contributed by brian d foy)
Starting with Perl 5.6.0, "glob" is implemented internally rather than relying on an external resource. As such, memory issues with "glob" aren't a problem in modern perls.
=====
Finally: The problem that was been reported was due to the misuse of glob by using it in a loop after it had already given all of the matched items. There was no problem with it.
Use The Source Luke and the Commit History
http://perl5.git.perl.org/perl.git/history/HEAD:/ext/File-Glob
update: While that long obsolete perlfaq5 item was there in 5.14 it is gone in the latest
I've just tested on Debian Wheezy with Perl 5.14.2.
sub test
{
my $dir = shift;
my $oldDir = cwd();
chdir($dir) or die("Could not chdir() : $!");
my $firstEntry = glob('*');
print "$firstEntry\n";
chdir($oldDir) or die("Could not chdir() : $!");
}
# /tmp/test1 (contains file1 and file2)
test('/tmp/test1); # Display file1 which is expected
# /tmp/test2 (contains file3 and file4)
test('/tmp/test2'); # Display file2 which is not expected
sub test
{
my $dir = shift;
my $oldDir = cwd();
chdir($dir) or die("Could not chdir() : $!");
(my $firstEntry) = glob('*');
print "$firstEntry\n";
chdir($oldDir) or die("Could not chdir() : $!");
}
# /tmp/test1 (contains file1 and file2)
test('/tmp/test1); # Display file1 which is expected
# /tmp/test2 (contains file3 and file4)
test('/tmp/test2'); # Display file3 which is expected
To resume here, glob buffer
is not flushed, even when we are out of caller scope.
With perl 5.22-1, both cases work as expected (scalar context).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With