![]() |
|
Syntactic Confectionery Delight | |
PerlMonks |
Re: Streaming to Handles (iterator)by tye (Sage) |
on May 05, 2004 at 23:26 UTC ( [id://350948]=note: print w/replies, xml ) | Need Help?? |
You don't need a stream; you want an iterator (yes, similar term). To turn this into an iterator in Perl5, you need to keep your own "stack". That is easy to do with an anonymous array (or two) inside your object. I put file names that I have yet to output into @{ $self->{files} } and output the next one from there the next time the iterator is called. I put directory names that I have yet to read the list of files from into @{ $self->{dirs} } and when there aren't any more file names to return, I read the next directory. First, here is how you'd use my iterator:
And here is the code that implements it:
I tested it enough to see that it appears to work just fine. If you had directories with huge numbers of files directly in them (not in subdirectories), then you might want to make the iterator a bit more complicated such that you don't keep a list of file names and instead return each file name (almost) immediately after you get it back from readdir (but I'm not sure I would recommend that). - tye
In Section
Seekers of Perl Wisdom
|
|