Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

strange File::Find thing?

by McMahon (Chaplain)
on Mar 22, 2004 at 20:31 UTC ( #338756=perlquestion: print w/replies, xml ) Need Help??
McMahon has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks... I nabbed some code from Merlyn: for a Win32 system like so:
{ my @starters = @ARGV ? @ARGV : 'F:/test'; @ARGV = (); find sub { if (($_ =~ ".c") or ($_ =~ ".h")) { push @ARGV, "$File::Find::dir/$_"; } }, @starters; } while (<>) { ... }
I assumed that this would populate @ARGV with only filenames, and not top-level directory names. However, subdirectory names are getting into @ARGV somehow:
Can't do inplace edit: F:/test/subdir is not a regular file at script. +pl line 28
Can anyone explain to me how those top-level subdirectory names got into @ARGV when I only ever pushed "$File::Find::dir/$_" onto the array? Thanks! -Chris

Replies are listed 'Best First'.
Re: strange File::Find thing?
by chip (Curate) on Mar 22, 2004 at 20:40 UTC
    You forgot to backwhack the dots in your patterns, and to anchor them at the end with a '$'. You're getting everything that contains 'h' or a 'c' (except as the first character).

    Incidentally, you'll need to double the backwhack and backwhack the '$' unless you use // for the pattern (which is recommended). So you should spell it: if (/\.c$/ || /\.h$/) { push ... }

    BTW, "$File::Find::dir/$_" is already available as $File::Find::name.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Thanks! I knew that regex looked odd, but I got fixated on the module. Have rearranged both attitude and code properly. -Chris
Re: strange File::Find thing?
by etcshadow (Priest) on Mar 22, 2004 at 20:38 UTC
    It's because
    • File::Find gives you back directories, as well as files, and you don't perform a -f $_ test to see if it is a file or not
    • you do a regexp like: $_ =~ ".c" which isn't doing what you think it is... for example "ac" will match that regular expression... you probably want: $_ =~ "\\.c", or perhaps even just /\.c/ which will do the same, but be more imediately recognizable.

    Update: fixed missing (second) backslash in the quotes.

    ------------ :Wq Not an editor command: Wq

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://338756]
Approved by Corion
Front-paged by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2018-02-21 11:45 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (279 votes). Check out past polls.