Writing a macro in Perl

Viewed 9357
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";
4 Answers
Related