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 /, /;

In reply to Re: Passing a string in a foreach loop by blazar
in thread Passing a string in a foreach loop by mat001

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.