use File::Find function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use File::Find function
# 1  
Old 11-10-2010
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";
}
}
# 2  
Old 11-10-2010
The syntax of the "find" function is incorrect. Do something like this -

Code:
$ 
$ 
$ 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
# 3  
Old 11-11-2010
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,
# 4  
Old 11-11-2010
Quote:
Originally Posted by new bie
...I notice you cat the script. ...
That was done to show you the content of my script "f27.pl". If I'd pasted the following -

Code:
$ 
$ 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
# 5  
Old 11-11-2010
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,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Find function script

Hi everybody, I want your help on what, I suppose, might seem a simple task to many of you. the deal is to write a script that will look in a specific folder to see if there is any file whose size is bigger than say 1M and if so to execute a command. like if then log_rotate fi ... (3 Replies)
Discussion started by: lenci_xc
3 Replies

3. Shell Programming and Scripting

find function

Hi, There is requirement where I need to search if a particular directory name is present or not and if its present I need to collate the paths of the directories in a file. I tried the below command echo $(find /home/mqm -name "check")>>config.txt Its saving the paths in one line(like... (7 Replies)
Discussion started by: jayii
7 Replies

4. Shell Programming and Scripting

use internal function with find command

Hi, I need to use a function in the find command to do some process on the file. I'm trying: funcname(){ ... } ... find ./ -name "*" -exec funcname {} \; But somehow this is not working. I don't want to have a separate script for whatever processing the function does. I want to have... (1 Reply)
Discussion started by: victorcheung
1 Replies

5. Shell Programming and Scripting

find function name in a program file

Hello All, Is there any way to find a function name in a program file using perl. for example, there is a file called Test.C in that . . void function1(..) { <some code> } int function2(..) { . . sam() /*RA abc100*/ ... .. .. xyz()/*RA abc201*/ .. (8 Replies)
Discussion started by: Parthiban
8 Replies

6. Shell Programming and Scripting

want to find out a function name in a cpp file

I have an error in my logs as it shows some function name . 1. I dnt know where is the file.cpp located only i know the machine . 2. How to find out that the function name is loacated in which path and which file into that machine. Thanks . (1 Reply)
Discussion started by: madfox
1 Replies

7. Shell Programming and Scripting

help with find function

Hi, Im using a find function to find a specific file and print it. eg. find /path -name filename -type f -print -exec more '{}' \; The problem I have is that I need to know if the file has not been fount (ie it does not exist). I had a look at the find function and could not find anything... (4 Replies)
Discussion started by: strobo
4 Replies

8. Shell Programming and Scripting

script to find function names

hi, i am a newbie and have to write a rather complicated script. Assume that i have a variable called x and a C source code file say file1.c (these are the inputs of the script) and i need to find the names of all the functions in the C file containing x.Take the following code as an example: ... (2 Replies)
Discussion started by: samantha grace
2 Replies

9. Shell Programming and Scripting

Function to find day of any given date.

Hi, Is there any function in Unix by which v can find the exact day of any given date. Like i need to get Wednesday if i give 05 07 2008 (format MM DD YYYY) Thanks, RRVARMA (5 Replies)
Discussion started by: RRVARMA
5 Replies

10. UNIX for Dummies Questions & Answers

find function

I was trying the following "find" command to search for a particular file in UNIX directories: find / -name <filename> -print Result on the screen appears like: find: cannot open <..> ..permission denied Is there any way, I could avoid these type of search results and only see the... (2 Replies)
Discussion started by: pkumar_syd
2 Replies
Login or Register to Ask a Question