Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there still unpredictable behavior with glob() in perl?

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.

like image 354
Stuart Woodward Avatar asked Oct 10 '12 08:10

Stuart Woodward


2 Answers

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

like image 141
so not liopa Avatar answered Nov 27 '22 06:11

so not liopa


I've just tested on Debian Wheezy with Perl 5.14.2.

Scalar context - Fail miserably

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

List context (Works as 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).

like image 36
Laurent DECLERCQ a.k.a Nuxwin Avatar answered Nov 27 '22 07:11

Laurent DECLERCQ a.k.a Nuxwin