in reply to Passing a string in a foreach loop

Are you sure that the values in @file are folder names and not file names? You may like to print each $f as it is processed and check that it was what you expected. Something like print "Processing >$f<\n" should do the trick. The > and < are there so you have a better chance of seeing leading and trailing whitespace and other nasty characters.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Passing a string in a foreach loop
by mat001 (Initiate) on Oct 20, 2005 at 03:56 UTC
    I did that and I am getting good data from the foreach $f (@file). The wield thing is that is put the same data outside of the foreach loop it works fine, but really I need it to work in the loop.

      Replace dir_size with a test sub that prints the param that is parsed in to it. dir_size really shouldn't care how it is called so there must be some difference in what is being passed in. A suitable test sub would be sub test {my $param = shift; print "param: >$param<\n";}


      Perl is Huffman encoded by design.

      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.
        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"; } }