Perl::...ArgOpen(3pm)

✨ NAME ✨

Okay so bestie, this is Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen β€” and it's here to tell you: write open $fh, q{<}, $filename; instead of open $fh, "<$filename";. That's the whole vibe. Three-arg open supremacy, no cap. πŸ’…

(And yeah, this is a page from the User Contributed Perl Documentation, so it's giving official-but-also-community. 🫢)


🌸 AFFILIATION 🌸

This Policy is part of the core Perl::Critic distribution. It's not some random third-party plugin you had to hunt down, she's built in, she's main character energy. πŸ‘‘


πŸ’– DESCRIPTION πŸ’–

The three-argument form of open (which, fun fact, was introduced in Perl 5.6) prevents subtle bugs that occur when the filename starts with funny characters like '>' or '<'. Lowkey this is a huge deal β€” imagine your filename is literally >output.txt and the old form just... opens it in write mode? Unhinged behaviour. 😭

The IO::File module provides a nice object-oriented interface to filehandles, which the author thinks is more elegant anyway. (And honestly? He's not wrong.)

open( $fh, '>output.txt' );          # not ok
open( $fh, q{>}, 'output.txt' );     # ok

use IO::File;
my $fh = IO::File->new( 'output.txt', q{>} ); # even better!

It's also more explicitly clear to define the input mode of the file, as in the difference between these two:

open( $fh, 'foo.txt' );       # BAD: Reader must think what default mode is
open( $fh, '<', 'foo.txt' );  # GOOD: Reader can see open mode

Notice how the good one is just... obviously correct? The reader doesn't have to guess. It's giving transparency. ✨

There is also a one-argument form of open which retrieves the expression to open from the global variable with the same name as the handle, but this has the same problems as the two-argument form, and adds in more ambiguity. So it's double bad. Like, why are we doing this to ourselves. πŸ’€

our $FH = '<foo.txt';
open( FH ); # not ok

This policy will not complain if the file explicitly states that it is compatible with a version of perl prior to 5.6 via an include statement, e.g. by having require 5.005 in it. So like, if you're out here living in the ancient times, she'll respect that and leave you alone. πŸ™ˆ


πŸŽ€ CONFIGURATION πŸŽ€

This Policy is not configurable except for the standard options. That's it. That's the tea. No knobs, no dials, no secret flags. She comes as she is. πŸ’…


πŸ¦‹ NOTES πŸ¦‹

There is one case in which you are forced to use the two-argument form of open: when doing a safe pipe open, as described in perlipc.

So don't come for this policy when you're doing a safe pipe open, okay? That's the one hall pass. 🫢

🌈 SEE ALSO 🌈

  • IO::Handle
  • IO::File

(Go read them, they're cute, they're useful, they're the whole reason this policy is so pressed about filehandles. ⭐)


πŸ‘‘ AUTHOR πŸ‘‘

Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>


Copyright (c) 2005-2021 Imaginative Software Systems. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


And that's the whole page, bestie! πŸ“ This lil gem is perl v5.42.0, dated 2024-10-23, part of the Perl::...ArgOpen(3pm) manpage situation β€” go forth and always use three-argument open, iykyk. 🧚✨

πŸ“¦ where this page comes from