Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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 test.pl 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

Comment on Noob File::Find issue
Download Code
Re: Noob File::Find issue
by GrandFather (Cardinal) 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 noname.pl 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 (Hermit) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-09-17 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (99 votes), past polls