FAQ 5.16 Is there a leak/bug in glob()?

FAQ 5.16 Is there a leak/bug in glob()?

am 24.12.2007 09:03:02 von PerlFAQ Server

This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

------------------------------------------------------------ --------

5.16: 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 memory leak and/or unpredictable behavior. It's best
therefore to use glob() only in list context.



------------------------------------------------------------ --------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Re: FAQ 5.16 Is there a leak/bug in glob()?

am 25.12.2007 15:13:06 von xueweizhong

> 5.16: 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 memory leak and/or unpredictable behavior. It's best
> therefore to use glob() only in list context.

It's still buggy in list context, see below. I filed this bug to p5p
weeks ago.

This works fine:

#! /bin/perl
$"=','; print join ',', glob "aaaa{@{[q{01}..q{05}]}}cccc"
__END__

aaaa01cccc,aaaa02cccc,aaaa03cccc,aaaa04cccc,aaaa05cccc


After make the range bigger enough, the bug occurs:

#! /bin/perl
$"=','; print join ',', glob "aaaa{@{[q{001}..q{999}]}}cccc"
__END__
aaaa

-Todd

Re: FAQ 5.16 Is there a leak/bug in glob()?

am 28.12.2007 01:06:21 von brian d foy

In article
<5fe6496b-8de5-4806-a106-8a42603b4d20@d21g2000prf.googlegroups.com>,
Todd wrote:

> > 5.16: 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 memory leak and/or unpredictable behavior. It's best
> > therefore to use glob() only in list context.
>
> It's still buggy in list context, see below. I filed this bug to p5p
> weeks ago.
>
> This works fine:
>
> #! /bin/perl
> $"=','; print join ',', glob "aaaa{@{[q{01}..q{05}]}}cccc"
> __END__
>
> aaaa01cccc,aaaa02cccc,aaaa03cccc,aaaa04cccc,aaaa05cccc

> After make the range bigger enough, the bug occurs:
>
> #! /bin/perl
> $"=','; print join ',', glob "aaaa{@{[q{001}..q{999}]}}cccc"
> __END__
> aaaa

This has already been explained, I think. It's a MAXPATHLEN limitation
that perl takes from your system.

http://groups.google.com/group/perl.perl5.porters/browse_thr ead/thread/2
40e1011be62ec5d/7e1b6958749ec3a9?lnk=st&q=glob+todd+perl#7e1 b6958749ec3a
9

These ranges work:

perl -le '$"=q|,|; print glob "aaaa{@{[q{000}..q{253}]}}cccc"'
perl -le '$"=q|,|; print glob "aaaa{@{[q{500}..q{753}]}}cccc"'

That is, until I shift the window, which doesn't work:

perl -le '$"=q|,|; print glob "aaaa{@{[q{1500}..q{753}]}}cccc"'

Then I try printing the length of the values I get back from glob. On
my machine, going past the upper bounds just gives back 'aaa'.

perl -le '$"=","; print length join "", glob
"aaaa{@{[q{000}..q{253}]}}cccc"'
2286
$ perl -le '$"=q|,|; print length join "", glob
"aaaa{@{[q{1000}..q{1202}]}}cccc"'
2233

It's not related to strictly related to total length though, because
shortening it doesn't let me extend the range that much (by one!):

perl -le '$"=q|,|; print length join "", glob
"a{@{[q{1000}..q{1203}]}}c"'
1224
perl -le '$"=q|,|; print length join "", glob
"a{@{[q{1000}..q{1204}]}}c"'
1