Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: Limiting a glob

by tirwhan (Abbot)
on Mar 09, 2009 at 18:13 UTC ( [id://749377]=note: print w/replies, xml ) Need Help??

in reply to Re: Limiting a glob
in thread Limiting a glob

(hopefully perl really buffers the filenames in the background and doesn't read them in all at once)

No, perl reads the entire list of files returned by glob into memory at once. Run the following to test this (WARNING may run for a long time or cause system resource problems on weakish machines).

#!/usr/bin/perl use strict; use warnings; mkdir "gtest" or die "screaming"; for (1..40000) { open my $f,">","gtest/$_" or die "gnashing"; close $f or die "howling"; } my $c=0; while (my $r=glob("gtest/*")) { $c++; if ($c == 1) { for (1..40000) { unlink "gtest/$_" or die "wailing" } } } print "$c\n"; rmdir "gtest" or die "exhausted";

I believe this is done so that glob returns a consisten snapshot of the directory contents as they were at some point, regardless of whether the content changes while you process the results. If you want more up-to-date data, with only current files being returned, you'll have to use opendir and readdir.

All dogma is stupid.

Replies are listed 'Best First'.
Re^3: Limiting a glob
by zod (Scribe) on Mar 09, 2009 at 18:19 UTC
    perl reads the entire list of files returned by glob into memory at once.

    This is what I originally assumed, hence my original question. So, I guess that makes the answer to my question, "No, you can't set a limit on a glob."


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://749377]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 18:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found