mat001 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a foreach loop with the Win32-DirSize PM. If I provide $f directly the script finds the size of the directory, however when I have a list of directories in a file and open the the script does not find the $f input.

Please help.

dir.txt = [ dir1.txt, dir2.txt, dir3.txt] use Win32::DirSize; open (FILE, "dir.txt") || die "can not open file\n"; chomp (@file = <FILE>); close FILE; #$f = "dir1"; # If I unpound this and pound out the foreach #loop work +s. foreach $f (@file) { my $Result = dir_size( $f, my $DirInfo, );

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

    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.
      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.
Re: Passing a string in a foreach loop
by blazar (Canon) on Oct 20, 2005 at 09:22 UTC
    dir.txt = [ dir1.txt, dir2.txt, dir3.txt] use Win32::DirSize; open (FILE, "dir.txt") || die "can not open file\n";

    it is a good habit and a common recommendation here and elsewhere to post code that at least compiles, (unless the problem is that it doesn't compile).

    What is that dir.txt bareword? Did you just forget to put the $ sigil in front of it? If so, then you're attempting to open a file called the same name as the stringification of an array ref.

    Or is it just a (poorly chosen) means to show the content of the file dir.txt? If so, then please think about a better way to do that...

    Two general recommendations that apply here are:

    1. help perl to help yourself and
      use strict; use warnings;
    2. do not slurp in files like that: here the file is tiny and it doesn't make much of a difference; but it's a bad habit.

    All in all this should work:

    #!Perl -l use strict; use warnings; use Win32::DirSize; open my $fh, '<', 'dir.txt' or die "Can't open `dir.txt': $!\n"; chomp, print dir_size $_ while <$fh>; __END__
    or even (if it's a quick hack)
    #!Perl -lp use strict; use warnings; use Win32::DirSize; BEGIN {@ARGV='dir.txt'} $_=dir_size $_; __END__
    But these both assume that dir.txt contains exactly one directory per line. If it contains lines of the form:
    dir1.txt, dir2.txt, dir3.txt
    as your code may suggest, then you would have to do something like
    print dir_size $_ for split /, /;