Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Noob File::Find issue

by bgreg (Initiate)
on Apr 30, 2012 at 22:33 UTC ( #968172=perlquestion: print w/replies, xml ) Need Help??
bgreg has asked for the wisdom of the Perl Monks concerning the following question:

I could use some help, I'm missing a fundamental somewhere. The following lines produce:
Undefined subroutine &main::find called at use FILE::Find; use warnings; find ( \&wanted, $directory); sub wanted{ print $File::File::dir; }
This is on active state Perl for Windows XP. Thanks in advance. - bGreg

Replies are listed 'Best First'.
Re: Noob File::Find issue
by GrandFather (Sage) on Apr 30, 2012 at 23:00 UTC

    The main issue is that Perl is case sensitive and that the module is actually File::Find. On a case insensitive file system you get weird results such as this because the module file gets found despite the error in the file name case, but the package isn't found.

    Note that you need $File::Find::dir to access the dir variable too. This should have been caught with warnings - you'd have gotten:

    Name "File::File::dir" used only once: possible typo at line + 12.

    when you ran the script.

    True laziness is hard work
Re: Noob File::Find issue
by thezip (Vicar) on Apr 30, 2012 at 23:01 UTC

    Heya bgreg,

    You've managed to capitalize the File in FILE::Find.

    Try this instead:

    use strict; use warnings; use File::Find; ... etc.

    Oh and BTW, make sure you use strict too!

    Welcome to Perl and Perlmonks!

    What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011
Re: Noob File::Find issue
by thomas895 (Chaplain) on May 01, 2012 at 01:53 UTC

    Also, not all modules export their subroutines. I have no idea if File::Find does(and I'm too lazy to check), so you might have to do one of the following:

    #These statements are equivalent use File::Find qw( find ); use File::Find 'find';
    I personally find it good practice(by my own conventions) to do that, just to make it clearer to myself where a function comes from. Your choice, ultimately.

    ~Thomas~ I believe that the source code to life is written in Perl :-)

      If I'm interested in knowing where stuff comes from I do exactly the opposite:

      use File::Find qw(); ... File::Find::find(...);

      By the time I'm a few dozen lines away from the top of the file I've probably completely forgotten where find() came from. A sometimes other benefit of being explicit is that if there is a 'find' in several different modules that are in use then it is clear which one is wanted.

      True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://968172]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-11-24 22:45 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (354 votes). Check out past polls.