in reply to list of fles in a directory, but use wildcards like *

In addition to the glob operator shown above, you can also do this checking in Perl, using its regular expression syntax instead of the local shell's: my @files = grep { /^onlythese.*\.txt$/ } readdir DIR;

Replies are listed 'Best First'.
Re: Re: list of fles in a directory, but use wildcards like *
by Limbic~Region (Chancellor) on Sep 04, 2003 at 16:42 UTC
    simonm,
    Just a nit

    While this works nearly 100% the same as the glob, it is not exactly the same. This is because some operating systems, *nix for instance, support imbedded newlines in file names. I would recommend adding the s modifier, using something like (\d|\D)* in place of .*, or just annotating that it won't match files with imbbedded newlines.

    Cheers - L~R

Re: Re: list of fles in a directory, but use wildcards like *
by thelenm (Vicar) on Sep 04, 2003 at 17:06 UTC

    To continue with Limbic~Region's nit, file names can even have newlines at the end, in which case using $ will cause the regex to match, for example, "onlythese1.txt\n". It will rarely if ever happen, but I tend to use \z to match the literal end of string, just in case.

    my @files = grep { /^onlythese.*\.txt\z/s } readdir DIR;

    -- Mike

    --
    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler