in reply to Re^2: Passing a string in a foreach loop
in thread Passing a string in a foreach loop

You might also like to post a version of the code that calls dir_size outside the loop as a sanity check that the calls are being made in the same way.


Perl is Huffman encoded by design.
  • Comment on Re^3: Passing a string in a foreach loop

Replies are listed 'Best First'.
Re^4: Passing a string in a foreach loop
by mat001 (Initiate) on Oct 20, 2005 at 04:16 UTC
    This works fine:
    use Win32::DirSize; open (FILE, "02_dir.txt") || die "can not open file\n"; chomp (@file = <FILE>); close FILE; $f = "d:\\programs"; #foreach $f (@file) { dsize($f); #} sub dsize { chomp (my $param = shift(@_)) ; print "$param \n"; my $Result = dir_size( $param, my $DirInfo, # this stores the directory information ); if ($Result == DS_RESULT_OK) { # If you don't want to display results in bytes, # let the module determine the best unit. my $Size = best_convert( my $SizeUnit, $DirInfo->{HighSize}, $DirInfo->{LowSize}, ); print "Dir size = $Size $SizeUnit \n"; } }
    This does not work.
    use Win32::DirSize; open (FILE, "02_dir.txt") || die "can not open file\n"; chomp (@file = <FILE>); close FILE; #$f = "d:\\programs"; foreach $f (@file) { dsize($f); } sub dsize { chomp (my $param = shift(@_)) ; print "$param \n"; my $Result = dir_size( $param, my $DirInfo, # this stores the directory information ); if ($Result == DS_RESULT_OK) { # If you don't want to display results in bytes, # let the module determine the best unit. my $Size = best_convert( my $SizeUnit, $DirInfo->{HighSize}, $DirInfo->{LowSize}, ); print "Dir size = $Size $SizeUnit \n"; } }

      Does the following code give the same result in both cases?

      use warnings; use strict; use Win32::DirSize; my $f = "d:\\programs"; print "Manual call\n"; dsize($f); my @file = ("d:\\programs"); print "Loop call\n"; foreach $f (@file) { dsize($f); } sub dsize { chomp (my $param = shift(@_)) ; print ">$param<\n"; #... }

      Note that you should always use strict; use warnings; to catch problems as soon as possible (I'm not implying they will help in this case, however ...)


      Perl is Huffman encoded by design.
        Thank you all that helped me with this problem. Like most problems it is the little things that screw up. The problem was in the dir.txt input file. I entered directories into the input file as \\\\\shaggy\\projects. This morning I changed the input file to read \\shaggy\projects and the code worked fine.
        Thank you again,
        mat001