Re: how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- From: xhoster@xxxxxxxxx
- Date: 29 Jun 2008 01:37:34 GMT
Ben Morrow <ben@xxxxxxxxxxxx> wrote:
[please quote properly]
Quoth news1234@xxxxxxx:
What would happen if I use follwing statement in perl"
foreach my $file (</home/*/.forward>){
do_something($file);
}
would perl
- iterate through the files
- or would perl first create a list of all the files and then
iterate through them.
'foreach' always creates a list and then iterates over it.
Not always. For example, in the case of foreach (1..1e6).
- or would it hit a linit and not provide all hits.
- or does it depend on the system perl is running on
You will eventually hit the memory limit on your system, and the limit
on the size of the pointer used to index the perl stack; you won't hit
any limits before that.
You can avoid pre-creating the list by using 'while' instead:
while (my $file = </home/*/.forward>) {
On my system, and I suspect on all systems, this still pre-creates the
result set in its entirety.
For example, if I put a "last" in the while loop, the code still performed
49418 "lstat" calls before it did a single loop iteration and broke out.
Perhaps the result set is stored in a special packed structure that is more
compact than it would be in a foreach loop. But a test shows that this
effect seems small. it took 12 meg to do
while (<blah/*>) {last}
and 14.3 meg to do
foreach (<blah/*>) {last}
Where blah has 49418 files in it.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
.
- Follow-Ups:
- Re: how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- From: Ben Morrow
- Re: how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- References:
- 'nobody' using sudo -- scary!
- From: Johnny
- Re: 'nobody' using sudo -- scary!
- From: RedGrittyBrick
- Re: 'nobody' using sudo -- scary!
- From: Big and Blue
- how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- From: nntpman68
- Re: how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- From: Ben Morrow
- 'nobody' using sudo -- scary!
- Prev by Date: FAQ 8.23 How can I open a pipe both to and from a command?
- Next by Date: Re: Need help with a question.
- Previous by thread: Re: how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- Next by thread: Re: how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- Index(es):
Relevant Pages
|