Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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
[stevieb]: I think that placing the * in C when dealing with pointers is akin to keeping separated the circumfix op from the inner ref in Perl: @{ $aref };, which is all personal preference. In perl, I'm good. C, don't know what I like
[stevieb]: ...yet. Still too newbie

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-01-24 02:33 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (200 votes). Check out past polls.