use strict;
my @array=('f1','f2','f3');
my $dir ='\tmp';
foreach (@array) {
my $FH = $_;
open ("$FH", ">$dir/${FH}.txt") or die $!;
}
foreach (@array) {
my $FH = $_;
close($FH);
}
i got "Can't use string ("f1") as a symbol ref while "strict refs" in use at bbb.pl line 6." error . What is the isuse ?
First: 2 arg open is bad, 3 arg open is better.
second, what on earth are you doing with open(“$FH” ..
argument 1 to open is supposed to be an actual filehandle of sorts which can be connected to a datastream. passing it a string will not work.
third
Forth:
dir =
\tmp? are you sure? I think you meant/tmp,\tmpis something different altogether.Fifth:
using strict is good, but you should use warnings too.
Sixth: Use names for variables that are explanatory, we know @ is an array @array is not more helpful.
ALL TOGETHER
Alternatively, depending on what you were trying to do, this may have been better code:
I don’t explicitly close $fh, because its not needed, as soon as $fh goes out of scope ( at the end of the block in this case ) it is closed automatically.