Phoenix.pm: I can't figure it out

Doug and Julie Miles dmiles at primenet.com
Fri May 7 12:55:15 CDT 1999


I am trying to get a friend interested in perl, and have been trying to
give him a small script that is close to something he wants to do.  The
idea is that he can modify it to do exactly what he wants, and thereby
learn some perl.  The problem is that I can't get it to work.  He is a C
programmer, so I took a script from the cookbook, and I've been 'C'ifing
it.  The problem is that the original version works, but mine doesn't.  I
was wondering if any of you can tell me why.  Its probably something
stupid, but right now I don't see what it is.  Both scripts are supposed to
recurse directories provided on the command line, and find the largest
file.  I'm doing this in Winbloze 95.  perl -v says:

This is perl, version 5.005_02 built for MSWin32-x86-object

Copyright 1987-1998, Larry Wall

Binary build 507 provided by ActiveState Tool Corp. http://www.ActiveState.com
Built 15:08:32 Nov 14 1998

Here is the original script:

use File::Find;
@ARGV = ('.') unless @ARGV;
my ($saved_size, $saved_name) = (-1, '');
sub biggest {
    return unless -f && -s _ > $saved_size;
    $saved_size = -s _;
    $saved_name = $File::Find::name;
}
find(\&biggest, @ARGV);
print "Biggest file $saved_name in @ARGV is $saved_size bytes long.\n";

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

The problem with my script is that it is returning the correct largest file
size, but the wrong file name (it is another file in the same directory as
the true largest file).  Just FYI, I was using '-s _' but was having
problems with it, so I replaced it with '(stat(_))[7]'.  Any clues are
greatly appreciated.  Thanks.

Doug and Julie Miles



More information about the Phoenix-pm mailing list