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

I'm trying to file glob a list of files into an array but I'm not having any luck. My first attempt:
@photos = <../images/albums/$directory/*.jpg>;
No dice.

My second attempt:

chdir "../images/albums/$directory/"; @photos = <*.jpg>;
...was also unsuccessful, both with and without the trailing slash in the first line. What obvious thing am I overlooking here?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar";
$nysus = $PM . $MCF;

Replies are listed 'Best First'.
(Ovid - reading files) Re: File globbing to a different directory
by Ovid (Cardinal) on Apr 25, 2001 at 02:00 UTC
    The following is untested. Does this answer your question?
    # Assign path to $somedir opendir DH, $somedir or die "Cannot open $somedir: $!"; my @photos = grep { /\.jpg$/i } readdir DH; closedir DH;

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      That did the trick...you are a Perl God in my book. I'll have to study those opendir and readdir functions. But I'm wondering: do you know why my approach failed?

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar";
      $nysus = $PM . $MCF;

        I have no idea why your approach failed. It looked fine to me. Now that I stop to think about it, I can't see why mine would have worked when your's failed. The benefit of using opendir is that it assigns to $! when it fails. File globs and the glob function don't do that (at least, not that I'm aware of).

        Side note: it's considered cleaner to check for files using the glob function instead of the file globbing operator. I should have suggested that first.

        When you said that it failed, how did it fail? Was there anything populating your @photos array?

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        Exactly, there was nothing in the array. I didn't get any errors either.

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar";
        $nysus = $PM . $MCF;