use File::Find function

Hello,

I'm learning the perl's Find function using unix but I keep getting this error when running the script:

"Not a CODE reference at /usr/lib.perl5/5.8.8/File/Find.pm line 822" - what does this mean?

Does anyone know??? Here's my script:

use File::Find;
find (\$dir,$ENV{HOME});

sub dir
{
if ($_ =~ /(pl)$/)
{
print "$_\n";
}
}

The syntax of the "find" function is incorrect. Do something like this -

$ 
$ 
$ cat -n f27.pl
     1    #!/usr/bin/perl -w
     2    use File::Find;
     3    find({wanted => \&dir}, $ENV{HOME});
     4    sub dir {
     5      /pl$/ && print $_,"\n";
     6    }
$ 
$ 
$ perl f27.pl
gui.pl
f12.pl
basic.pl
f27.pl
tst.pl
tsdiff.pl
sample.pl
get_counts.pl
$ 
$ 

tyler_durden

thank you Tyler,

I notice you cat the script. I was using vi editor to write that script. Does that mean I can't use vi editor to write my script when it comes to Find command? I had #!/usr/bin/perl -w on top of my script but didn't type in above, but still got that error message.

new bie,

That was done to show you the content of my script "f27.pl". If I'd pasted the following -

$ 
$ perl f27.pl
gui.pl
f12.pl
basic.pl
f27.pl
tst.pl
tsdiff.pl
sample.pl
get_counts.pl
$ 
$ 

your (or anyone's) first question would've been - "huh?? what the heck is f27.pl ?"

By displaying
(a) the script content and then
(b) the execution of the script

I've tried to make the testcase complete. You see the script first and then you see it in action.
In fact, in most cases, the input (which was left out here) should also be included. So then you have:

(1) the input
(2) the algorithm/program
(3) the output

to complete the picture.

My post tries to tell you - "here's my homegrown script that I've named f27.pl and here is what I see when I execute it... so it works fine on my system...".
Of course, that is just a guide, an indication. Just because it works on my system doesn't mean it would work on yours. Quite possibly, a million things could be different on your system. Different doesn't mean wrong. It means just that - different; nothing more, nothing less. Taking that post as the directive, the onus of finding out what's different is on you.

tyler_durden

Hi Durden_Tyler,

I understood your script above- your code named f27.pl and then you run it by perl f27.pl to get the output below it.

What got me confused was I wrote the same code above using vi editor- vi f27.pl, then the code, and then I go perl f27.pl and I got the message "Not a CODE reference..."

I'm just wondering if I can use vi editor when it comes to File::Find command?

Thanks,