open $FP, '>', $outfile or die $outfile." Cannot open file for writing\n";
-
I have this statement a lot of times in my code.
-
I want to keep the format same for all of those statements, so that when something is changed, it is only changed at one place.
-
In Perl, how should I go about resolving this situation?
-
Should I use macros or functions?
I have seen this SO thread How can I use macros in Perl?, but it doesn’t say much about how to write a general macro like
#define fw(FP, outfile) open $FP, '>', \
$outfile or die $outfile." Cannot open file for writing\n";
First, you should write that as:
including the reason why
openfailed.If you want to encapsulate that, you can write:
Starting with Perl 5.10.1, autodie is in the core and I will second Chas. Owens’ recommendation to use it.