Hi,

I'm having problem with the usage of File::Find Module. What i'm trying to do is to browse through two directories and put all the files into two arrays respectively (each array for each dir). I just cant use the "find" twice... An error message will be shown:

Use of uninitialized value in subroutine entry at C:/Perl/lib/File/Find.pm line
849.
Can't use string ("") as a subroutine ref while "strict refs" in use at C:/Perl/
lib/File/Find.pm line 849.

my coding is as below:

#!/usr/bion/perl
#Path_Header_files_Extract.pl

use warnings;
use strict;
use File::Find;

my $fileH;
my $fileH2;
my $header_dir1 = "C:/ProgramFiles/X/S/";
my $header_dir2 = "C:/ProgramFiles/Z/R/";
my $current_header;
my @header_files;
my @header_files_path;
my @temp_all_path_user;
my @temp_all_path_vendor;
my @not_in_UserSDK;
my $index = 0;
my $counter = 0;

open fileH, "Header_Used.txt" or die $!;
while(<fileH>)
{
    push @header_files, $_;
}

close fileH;

for (@header_files)
{
    chomp ($_);
}

for (@header_files)
{
    print ("$_\n");
}


find( {wanted=> \&wanted, no_chdir => 1}, $header_dir1 );

sub wanted
{
    push @temp_all_path_user, "$_";
}

for $1 (@temp_all_path_user)
{
    for $2 (@header_files)
    {
        if ($1 =~ /$2/i)
        {
            push @header_files_path, $1;
            last;
        }
    }
}

find( {wanted2=> \&wanted2, no_chdir => 1}, $header_dir2 );
sub wanted2
{
    push @temp_all_path_vendor, "$_";
}

@header_files_path = sort @header_files_path;

for $3 (@header_files_path)
{
    print "$3\n";
    $counter++;
}

print $counter;

Recommended Answers

All 5 Replies

I think the problem is your use of 'wanted2' as the name for the callback function. I think it has to be 'wanted'. Just use "wanted" and ignore the warning about "sub wanted redefined".

It works fine now. But will the warning affects the program? Is there anyway to eliminate it? I tried to use argument in the "sub wanted" but it show errors.

The warning will have no affect on the program. If you don't want the warning issued you can elect not to use the warnings pragma or use the "no warnings" switch in a block of code. See the warnings pragma documentation for details.

http://perldoc.perl.org/warnings.html

Thanks. :)

Hi,

After i tried to run the program a few time, there seems to be something wrong. If i define the wanted twice, only the second sub wanted will be loaded. Even when i'm actually trying to call the first sub wanted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.