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

Is it possible to provide unlink a directory path with a regex something like this: ?
unlink <"$dir\\$ip_address.*">;
I'd rather not open the dir and retrieve all the contents and iterate over each one.

Replies are listed 'Best First'.
Re: unlink with regex?
by ikegami (Patriarch) on Apr 09, 2009 at 15:58 UTC

    I'd rather not open the dir and retrieve all the contents and iterate over each one.

    Do you mean you don't want this to happen, or do you mean you don't want to code it yourself?

    If the former, you're out of luck. That's the only way to get a directory listing.

    If the latter, what you want is called globbing. Be sure to use File::Glob's bsd_glob rather than the builtin glob. While both are implemented by the same function, their handling of spaces differs. bsd_glob will handle them better.

    You can also do nice things with File::Find::Rule name filter if you want a recursive solution.

Re: unlink with regex?
by kyle (Abbot) on Apr 09, 2009 at 15:24 UTC
Re: unlink with regex?
by almut (Canon) on Apr 09, 2009 at 15:31 UTC

    The syntax for <...> glob expressions is "shell wildcard" (not a Perl regex), i.e. your .* probably should be * — unless you want to match the dot literally.  Also, you probably don't want the quotes.

Re: unlink with regex?
by CountZero (Bishop) on Apr 09, 2009 at 15:56 UTC
    I'd rather not open the dir and retrieve all the contents and iterate over each one
    It may be hidden from view but the contents of the folder will have to be retrieved somehow in order to apply the regex to it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: unlink with regex?
by Bloodnok (Vicar) on Apr 09, 2009 at 15:51 UTC
    According to I/O Operators, your use of <...> seems to be, if not deprecated, at the very least, not recommended.

    unlink glob "$dir\\$ip_address.*";

    would appear to be the way to go - see glob (which apparently, uses File::Glob - as suggested by kyle) & unlink.

    Update:

    Many thanx to FunkyMonk - for both pointing out the broken link and suggesting a fix ... link to I/O operators mended

    A user level that continues to overstate my experience :-))