in reply to Passing a string in a foreach loop
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:
- help perl to help yourself and
use strict; use warnings;- 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:
or even (if it's a quick hack)#!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__But these both assume that dir.txt contains exactly one directory per line. If it contains lines of the form:#!Perl -lp use strict; use warnings; use Win32::DirSize; BEGIN {@ARGV='dir.txt'} $_=dir_size $_; __END__as your code may suggest, then you would have to do something likedir1.txt, dir2.txt, dir3.txtprint dir_size $_ for split /, /;
- Comment on Re: Passing a string in a foreach loop
- Select or Download Code