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

Hi Monks,
i am trying to find all '.dll' files and '.exe' files stored in a directory(this directory may also contain other files) and storing those extracted files in say @array_files and printing them..

i am using the following code for that..

the problem is........
i am able to extract only the files that i explicitly mentioned the full name of the file given to the file variable $name below.
i.e only test1.dll files are extracting other names with .dll are not coming..
if i use $name = "/*.dll|*.exe/" to extract my required .exes and .dlls.. the module is not at all looking into and matching the pattern and failing at 'if' condition..
shall i have to use other module for this req.. or is any other way to do using File::Find?
#!/usr/bin/perl use File::Find; $dir = "."; $name = "test1.dll"; filefind($name); sub filefind { # my $aName = @_; # my $file = $File::Find::aName; my @dirs = ($dir); find(\&mysub, @dirs); return @files; } print "files are @files\n"; sub mysub { if ($_ =~ $name) { push @files, $_; } }
thanks®ards,
pavan

fixed formatting by holli

Replies are listed 'Best First'.
Re: How to extract Req files into some other directory using Modules
by holli (Abbot) on Aug 03, 2005 at 10:19 UTC
    my $rule = File::Find::Rule->new; $rule->file; $rule->name( '*.dll', '*.exe' ); my @files = $rule->in( '.' );


    holli, /regexed monk/
Re: How to extract Req files into some other directory using Modules
by blazar (Canon) on Aug 03, 2005 at 10:12 UTC
    If you didn't post as an anonymous monk, I would hav /msg'ed this to you rather than posting a reply: your node is hardly readable at all. Put code tags arounf your code, at least.

    Did you notice that after pressing the preview button you were told " If something looked unlike you expected it to you might need to check out Writeup Formatting Tips". So I must suppose it did look like you expected, shouldn't I?

    Update: now that your post has been fixed, I can answer to you...

    i.e only test1.dll files are extracting other names with .dll are not coming..
    if i use $name = "/*.dll|*.exe/" to extract my required .exes and .dlls.. the module
    You're confusing shell patterns with regexen. Check perldoc perlre. Or else, you may try the find2perl command for a starter.
    #!/usr/bin/perl use File::Find;
    As a general rule, you're missing the two most important lines in your program: maybe it won't seem so at first, but make your life easier and help people (not to say perl!) to help you by writing
    use strict; use warnings;
    as well.
    sub filefind { # my $aName = @_; # my $file = $File::Find::aName; my @dirs = ($dir); find(\&mysub, @dirs); return @files; }
    really, I don't see the need for this wrapper sub. As I don't see the need for many intermediate variables you're putting in many places - since they do not contribute to readability and are at most confusing.
    if ($_ =~ $name) { push @files, $_; }
    Two things:
    1. the topicalizer is just there to be the implicit topic: use it as such or else use explicit variable names,
    2. here you have $_ =~ $name, but $name is a string: what is happening is that perl converts it to a regex. This is not generally what you want and may have an unexpected outcome for you if $name contains charachters that have a regex special meaning (which it does happen to have here, but for a "fortunate" coincidence it doesn't happen to have any "surprising" effect in this case).
Re: How to extract Req files into some other directory using Modules
by anonymized user 468275 (Curate) on Aug 03, 2005 at 10:21 UTC
    Two things. The regexp is using shell wildcarding instead of perl wildcarding, which when fixed would be:
    $name = "(\.dll|\.exe)\$";
    and the regexp needs to be delimited where it is used rather than in the variable, i.e.
    $try =~ /$name/;

    One world, one people

      $name = "(\.dll|\.exe)\$";
      As a minor point, I wouldn't use capturing parens where not needed, I'd write
      my $name = qr/\.(?:dll|exe)$/;
      instead. And as you can see from this line itself, I wouldn't put into a string to interpolate it later, but I would create Regex object, especially since it isn't supposed to change. But then I wouldn't probably create any intermediate variable, in this case, that is:
      find sub { push @files, $_ if /\.(?:dll|exe)$/ }, @dirs;
Re: How to extract Req files into some other directory using Modules
by Tomtom (Scribe) on Aug 03, 2005 at 10:22 UTC
    I don't think
    *.exe|*.dll
    will be considered as a valid Perl regex. You should try the following instead :
    ^.*\.(?:exe|dll)$