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

Is there a way to use a wild card for a file name with the copy module.

use File::Copy;
copy ( "demon*.log", 'd2.log' ) or die "Can't copy.";
Since the "demon*.log" file might have a data, I won't know what is the exact lettering.

Or is there a better way to doing this?

Thanks.
  • Comment on How to use file copy with wild card characters

Replies are listed 'Best First'.
Re: How to use file copy with wild card characters
by Zaxo (Archbishop) on Dec 31, 2004 at 18:57 UTC

    See the glob function.

    After Compline,
    Zaxo

Re: How to use file copy with wild card characters
by thor (Priest) on Dec 31, 2004 at 19:12 UTC
    What if the wildcarded filename matches more than one file? What is the expected behavior?

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      File::Copy won't function without a $to and $from argument, so some naming convention for the new files is needed. I would approach it in this manner:

      map { copy($_, $_ . '.new') } glob("*.ext");
        While I agree that that is probably a good idea, it doesn't answer the question that I asked. It could be that the OP only wants to save one archived log. So, in that case, you could move every file in the glob to a static file name i.e.
        foreach my $file (glob('*.log')) { rename $file, "oldlog" or die "Oops: $!"; }
        I don't know about you, but I'm not psychic.

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

        Danger, Will Robinson! map in void context! I would write that like this instead:

        copy($_, "$_.new") for glob "*.ext";

        I think it's much easier to look at.