Firstly, because I often fall into the exact same trap that you did, I like to do something like this:
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 :-)use Cwd; ... my $dir = getcwd; my @images = map { "$dir/$_" } readdir(DIR);
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:
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.@images = grep { -f && # only allow files, not directories /\.(png|gif|jpe?g|.....)$/i } map { ... } readdir(DIR);
In reply to Re: Images::Size troubles
by DrHyde
in thread Images::Size troubles
by dstefani
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |