Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Opendir, readdir & closedir.

by perlinux (Deacon)
on Apr 30, 2004 at 14:11 UTC ( [id://349406]=perlquestion: print w/replies, xml ) Need Help??

perlinux has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I'm making a little script with the simple Imager module.
I'm processing my images to resize them, and when the width is minor than the height I open the image with Gimp to make a simple transformation. I read all the images in my directory with:
opendir THIS, "." or die "Error: $!"; my @allimages = grep /\.jpg$/i , readdir THIS;
and after Gimp processing, I rename these images (temp_$number.jpg), and again the code is:
my @renamedimages = grep /^temp_\d\d+\.jpg$/i, readdir THIS;
but @renamedimages is empty.
It works if I write closedir THIS; after the first readdir, and after reopen with opendir. Why?
Is it impossible to have one opendir, many readdir and finally one closedir?
In Perl Programming, section Functions, there's a description for these functions, but this problem is not resolved.
Thanks

Replies are listed 'Best First'.
Re: Opendir, readdir & closedir.
by hv (Prior) on Apr 30, 2004 at 14:22 UTC

    With a directory handle, it starts at the beginning of the directory (when you open it with opendir()), then proceeds through the directory when you call readdir() until it gets to the end, and then it stops.

    If you want to rewind the handle back to the beginning of the directory you need rewinddir.

    Hugo

Re: Opendir, readdir & closedir.
by coec (Chaplain) on Apr 30, 2004 at 14:23 UTC
    IIRC you need to use rewinddir. The reason is that you have read the entire directory, i.e you're pointing at the end of the directory list and you need to rewind to the beginning, or course you can do the whole opendir thing again.

    You can have single opendir/closedir with many readdirs if you use rewinddir.

    CC

Re: Opendir, readdir & closedir.
by matija (Priest) on Apr 30, 2004 at 14:23 UTC
    perldoc -f readdir says: Returns the next directory entry for a directory opened by "opendir". If used in list context, returns all the rest of the entries in the directory. If there are no more entries, returns an undefined value in scalar context or a null list in list context.

    That means it's behaving exactly as documented: the grep called it in list context, it returned a list of all files, and now it will be returning a NULL list until you do an opendir again.

    It's the same thing as trying to read from a file where you've already read all the contents.

Re: Opendir, readdir & closedir.
by flyingmoose (Priest) on Apr 30, 2004 at 19:03 UTC
    I'm processing my images to resize them, and when the width is minor than the height I open the image with Gimp to make a simple transformation. I read all the images in my directory with:

    Your question is perfectly valid, but just as an FYI, the image gallery and resizing thing is one of the world's most reinvented wheels. Maybe people just like coding Perl, and nothing is wrong with that... but I suggest you check out igal or if you want, the ImageMagick modules, or maybe even just the ImageMagick convert utility. Plenty of solutions already out there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://349406]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-16 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found