Phoenix.pm: I can't figure it out (Shay's reply)

Doug and Julie Miles dmiles at primenet.com
Fri May 7 18:49:05 CDT 1999


This bounced for some reason.  This is Shay's reply.  Bryan also came to
the same solution.  Thanks to Shay and Bryan.

Shay said:

It has to do with the '_' filehandle that is causing the problems. I think
since you reference it twice in the 'biggest' sub, it is always a file behind
what it should be?


If you make the following change, it works fine:

sub biggest{ 

  if(-f $_){   ## Change '_' to '$_'

    print "$_ $File::Find::name: " . (stat(_))[7] . "\n"; 
  } 
 
  if((-f _) && ((stat(_))[7] > $file_size)) { 
      $file_size = (stat(_))[7]; 
      ($file_name = $File::Find::name) =~ s!/!\\!g; 
     print "Biggest file $file_name is $file_size bytes.\n"; 
  } 
 
} 


This will give you the biggest file in the current tree, not directory. Anyway
the problem lies in the special filehandle you are using. The File.pm uses
this
same filehandle so you may be clashing with it. You may want to check out the
symbol tables by dumping the File object and printing out the current value in
your program to see what's going on.

If you do the following you will see what I mean:


sub biggest{ 

  if(-f _){   ## Change '_' to '$_'
    print "",(stat(_))[7]," => ",(stat($_))[7],"\n";
    #print "$_ $File::Find::name: " . (stat(_))[7] . "\n"; 
  } 
 
  if((-f _) && ((stat(_))[7] > $file_size)) { 
      $file_size = (stat(_))[7]; 
      ($file_name = $File::Find::name) =~ s!/!\\!g; 
     #print "Biggest file $file_name is $file_size bytes.\n"; 
  } 
 
}


You'll see that _ is not equal to $_ as you may think. I tried outputting _ as
a filename and the only way I could get at it was to declare the following
${_}
which is equivalent to $_.





Shay





>here is my version:
>
>use File::Find;
>
># If no command-line arguments are provided, set to the current directory.
>if(!@ARGV)
>{
>
> @ARGV = ('.');
>
>}
>
>my $file_name = '';
>my $file_size = -1;
>
>find(\&biggest, @ARGV);
>
>print "Biggest file $file_name is $file_size bytes.\n";
>
># biggest
>######################################################################
>
>sub biggest
>{
>
>  # This is just for debugging info.
>  if(-f _)
>  {
>
>    print "$_ $File::Find::name: " . (stat(_))[7] . "\n";
>
>  }
>
>  if((-f _) && ((stat(_))[7] > $file_size))
>  {
>
>    # Get the largest file's filename and size.
>    $file_size = (stat(_))[7];
>    ($file_name = $File::Find::name) =~ s!/!\\!g;
>    print "Biggest file $file_name is $file_size bytes.\n";
>
>  }
>
>} # END: biggest


Doug and Julie Miles



More information about the Phoenix-pm mailing list