Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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: http://www.perlmonks.com/index.pl?node_id=115428 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

Comment on strange File::Find thing?
Select or Download Code
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.
    Enjoy.

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

    ------------ :Wq Not an editor command: Wq
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://338756]
Approved by Corion
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-11-29 02:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls