Seeing that you're trying to learn, I'll make a couple of suggestions for ways your script could be improved.

Firstly, because I often fall into the exact same trap that you did, I like to do something like this:

use Cwd; ... my $dir = getcwd; my @images = map { "$dir/$_" } readdir(DIR);
which turns the list of directory entries into a list of unambiguous fully-qualified names. map takes an action (in this case a little block of code) and a list on its right hand side, spitting another list out on the left which has been transformed using the specified action. See the docs for a more lucid explanation than I can give :-)

Second, readdir(DIR) gives you a list of everything in the directory - image files, other files, subdirectories, plus the two special directory entries '.' and '..'. You can easily winnow the list down like so:

@images = grep { -f && # only allow files, not directories /\.(png|gif|jpe?g|.....)$/i } map { ... } readdir(DIR);
grep is sort of like map, except that instead of transforming the list, it filters the list. The list it spits out on its left-hand side will consist solely of those elements of the input list for which the action evaluates true - in this case, will consist solely of those elements which are filename as opposed to directory names, and whose filenames end in .png, .gif, .jpeg, .jpg etc. Again, see the docs for a full explanation. map and grep are very powerful tools and they save my bacon day after day.

In reply to Re: Images::Size troubles by DrHyde
in thread Images::Size troubles by dstefani

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.