Reputation: 482
Why does use strict
not let me open and write in a file as in the code below? Commenting out use strict
works perfectly.
use strict;
use warnings;
my $filename = "file_abc.txt";
open($fh, '>', $filename) or die("Couldn't open $filename\n");
print $fh "ABC";
print $fh "DEF\n";
print $fh "GHI";
close $fh;
Upvotes: 1
Views: 306
Reputation: 62236
When using strict
, you typically declare each variable with my
:
open(my $fh, '>', $filename) or die("Couldn't open $filename\n");
You can get more information about some error messages by using use diagnostics;
:
Global symbol "$fh" requires explicit package name at ...
Execution of ... aborted due to compilation errors (#1)
(F) You've said "use strict" or "use strict vars", which indicates
that all variables must either be lexically scoped (using "my" or "state"),
declared beforehand using "our", or explicitly qualified to say
which package the global variable is in (using "::").
Upvotes: 8