Perl::...andles(3pm)

🌸 NAME 🌸

Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles β€” write open my $fh, q{<}, $filename; instead of open FH, q{<}, $filename;. That's the whole assignment. πŸ’–

This is the Perl::...andles(3pm) page from User Contributed Perl Documentation, and it's literally here to help you level up your filehandle game, bestie. πŸ’…

🫢 AFFILIATION 🫢

This Policy is part of the core Perl::Critic distribution. It's not a random third-party side quest β€” fr fr, it's main character energy in the Critic universe. πŸ‘‘

✨ DESCRIPTION ✨

Using bareword symbols to refer to file handles is particularly evil because they are global, and you have no idea if that symbol already points to some other file handle. Vibe check: that's giving "shared locker energy" but for filehandles, and I am not okay. 😭 You can mitigate some of that risk by "local"izing the symbol first, but that's pretty ugly. Ngl, lowkey tragic.

Since Perl 5.6, you can use an undefined scalar variable as a lexical reference to an anonymous filehandle. Obsessed with a clean little my $fh. Alternatively, see the IO::Handle or IO::File or FileHandle modules for an object-oriented approach. Slay. πŸ¦‹

open FH, '<', $some_file;           #not ok
open my $fh, '<', $some_file;       #ok
my $fh = IO::File->new($some_file); #ok

There are three exceptions: STDIN, STDOUT and STDERR. These three standard filehandles are always package variables. They're the VIPs, they get a pass. πŸŽ€

This policy also applies to the sysopen function as well. Yes, sysopen too, bestie. 🫑

πŸŽ€ CONFIGURATION πŸŽ€

This Policy is not configurable except for the standard options. So no custom little knobs and dials here β€” highkey just the standard options. Periodt. πŸ’…

🌈 SEE ALSO 🌈

  • IO::Handle
  • IO::File

iykyk, these are the modules to check out. πŸ¦‹

🧚 AUTHOR 🧚

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

Copyright (c) 2005-2022 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.


xoxo, this page came from User Contributed Perl Documentation, manual name Perl::...andles(3pm), served by perl v5.42.0, dated 2024-10-23 β€” mwah πŸ’‹πŸ’–

πŸ“¦ where this page comes from