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

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.
  • Comment on Re^2: Passing a string in a foreach loop

Replies are listed 'Best First'.
Re^3: Passing a string in a foreach loop
by GrandFather (Saint) on Oct 20, 2005 at 04:05 UTC

    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.
Re^3: Passing a string in a foreach loop
by GrandFather (Saint) on Oct 20, 2005 at 04:08 UTC

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

        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.