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

I need to open a file with an unknown digit in the filename. The mentioned character is always in the same place. What's wrong with the following?: open (CACHE_FILE, "</home/www/cache002/000000\_999999\_/d{1}\-en") || die ("Can't open cachefile");
  • Comment on Wildcards - open a file with one unknown digital character

Replies are listed 'Best First'.
Re: Wildcards - open a file with one unknown digital character
by almut (Canon) on Jan 09, 2010 at 01:14 UTC

    open expects a file name/path, not a wildcard or regular expression (think of it this way: What should it do in case more than one file would match? Open several files to one handle?).

    Use glob to expand a wildcard expression (the single unknown digit would be [0-9]), and then open one of the matching file names.

      glob() works great! thank you
Re: Wildcards - open a file with one unknown digital character
by ikegami (Patriarch) on Jan 09, 2010 at 01:38 UTC

    By the way, escaping _ and - is unnecessary in string literals, in regex patterns and in glob patterns. Escaping them is just visually confusing.

    Also, by simply appending : $! to your error message, you make it far more useful.

      Thank you for the hints. I escaped _ because of the scalars containing _ in their names. if I use my @cachefiles = glob("/home/cache/*_$scalar_1_2"); it does not recognize $scalar_1 I'd have to write my @cachefiles = glob("/home/cache/*_$scalar_1\_2");
        "...${scalar}_..."
        would also do. The curly method also works where a slash wouldn't.
        "...${abc}def..."