http://qs1969.pair.com?node_id=1102164

wazoox has asked for the wisdom of the Perl Monks concerning the following question:

Hi friends, I can't get my head around this. Here is a script that's supposed to fill a hash from a file ( formatted like "someKey" => "someValue" ) and search for each key through a code directory, to determine which keys are obsolete and should be removed from the file.

But the script doesn't work. Basically it behaves exactly like if it went through a few files at a time, then exits; then when launched again it will parse the next following files. Maybe it's beacause it's late, maybe I really don't understand File::Find, anyway I don't understand what's happening.

#!/usr/bin/perl use strict; use warnings; use File::Find; use Data::Dumper; use 5.010; my %master; ################### sub showhelp { print qq( Usage : $0 lang code_directory \n); exit; } ################## my ( $master, $mydir ) = @ARGV; if ( not $master && $mydir ) { showhelp(); } open my $masterf, '<', $master or die "can't open $master"; while (<$masterf>) { if (/"(\w+)"\s*=>\s*"(.+)",/) { $master{$1} = 0; } } close $masterf; find( \&findFile, $mydir ); sub findFile { return unless -f; searchfile($_); } sub searchfile { my $file=shift; # print "open $file\n"; open my $foundfile, '<', $file or die "can't open $file"; my $first=<$foundfile>; foreach my $k ( keys %master ) { while ( my $line=<$foundfile> ) { if ($line =~ m/$k/ ) { $master{$k}++ ; # print "$k found in $file\n"; } } } close $foundfile; } foreach my $k ( keys %master ) { print "$k NOT USED\n" if $master{$k} == 0; }

Replies are listed 'Best First'.
Re: Making sense of File::Find
by dasgar (Priest) on Sep 26, 2014 at 18:20 UTC

    I think you may have some flawed logic in your "searchfile" subroutine. You have:

    foreach my $k ( keys %master ) { while ( my $line=<$foundfile> ) {

    At the end of the first iteration through the foreach loop, you will have checked every line in the file. But every iteration of the foreach loop after that won't execute the while loop.

    One option to is to open the file inside the foreach loop and then work through the while loop and then close the file in each iteration like this:

    sub searchfile { my $file=shift; # print "open $file\n"; foreach my $k ( keys %master ) { open my $foundfile, '<', $file or die "can't open $file"; my $first=<$foundfile>; while ( my $line=<$foundfile> ) { if ($line =~ m/$k/ ) { $master{$k}++ ; # print "$k found in $file\n"; } } close $foundfile; } }

    That's basically doing this. For every key in the hash, do the following: open the file, skip the first line, search the rest of the lines for the key (and do action if found) and close the file

    Another option is to swap your foreach and while loops like this:

    sub searchfile { my $file=shift; # print "open $file\n"; open my $foundfile, '<', $file or die "can't open $file"; my $first=<$foundfile>; while ( my $line=<$foundfile> ) { foreach my $k ( keys %master ){ if ($line =~ m/$k/ ) { $master{$k}++ ; # print "$k found in $file\n"; } } } close $foundfile; }

    This is basically doing the following: open the file, skip the first line, then for each line check to see if the matches any of the keys in the hash (and if it does, do some action) and then closes the file.

    Not knowing more information about your files and their contents, I think I would personally opt to go the second route, which only opens each file once.

      D'oh, of course, here's what's happening when you're working late :)
Re: Making sense of File::Find
by zentara (Archbishop) on Sep 26, 2014 at 18:23 UTC
    #!/usr/bin/perl # Recursively searchs down thru directories replacing patterns in file +s. # usage zsr 'search' 'replace' 'ext'(optional with no .) # use '' for null string in $replace # ex: zsr 'type1' 'series A' use warnings; use strict; use File::Find; my ($search,$replace,$ext) = @ARGV; if (defined $ext) {$ext = ".$ext"} else {$ext = '.*'}; die "Usage : zsr 'search' 'replace' 'extension' (extension optional)\n +" if ($search eq ""); find (\&wanted, "."); sub wanted { my $open = $_; my $tempfile = 0; if (!($open =~ /$ext$/i) or (-d||-B||-l)) {return} print $open,"\n"; my $mode = (stat $open)[2]; #print $mode,"\n"; #printf "Permissions are %04o\n", $mode & 07777; open (TEMP,">> $tempfile"); open (FH, "< $open") or die "Can't open $open: $!\n"; while (<FH>) { $_ =~ s/$search/$replace/g; print TEMP $_; } close FH; close TEMP; rename ($tempfile, $open) or die "Can't rename $open: $!\n"; chmod ($mode,$open) or die "Can't restore permissions to $open, possib +ly wrong owner: $!\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Making sense of File::Find
by Anonymous Monk on Sep 26, 2014 at 23:00 UTC
    If you do
    searchfile ( $one ); searchfile ( $two ); searchfile ( $three ); showMaster( \%master );
    you can see that File::Find isn't the culprit