Why is three-argument open calls with autovivified filehandles a Perl best practice?

  • Using typeglobs for filehandles (like OUT) is not a good idea, as they are global across your entire program – you need to be sure that no other routine including those in modules are using the same name (including in the future).
  • Using the two-argument form of open exposes your application to mis-behaviour caused by variables containing special characters, for example my $f; open $f, ">$some_filename"; is exposed to the bug where $some_filename containing a leading > will change the program’s behaviour.

Using the three-argument form avoids this by separating the mode and filename into separate arguments where they can’t interfere.

Moreover, using the lots-of-arguments form with pipes is a very good idea:

open $pipe, '|-', 'sendmail', '[email protected]';

Is better than doing it all as a single string – it avoids possible shell injection etc.

Leave a Comment