Perl::...Handles(3pm)

🌸 NAME 🌸

okay bestie, this is Perl::...Handles(3pm) β€” yes, that's the manual name β€” from the User Contributed Perl Documentation, and it's here to fix your dir-handle situation. The actual policy is Perl::Critic::Policy::InputOutput::ProhibitBarewordDirHandles, and the whole assignment is: write opendir my $dh, $dirname; instead of opendir DH, $dirname;. slay. πŸ’…

πŸŽ€ AFFILIATION πŸŽ€

This Policy is part of the core Perl::Critic distribution. ✨

πŸ”₯ DESCRIPTION πŸ”₯

okay so using bareword symbols to refer to directory handles is particularly evil because they are global, and you have no idea if that symbol already points to some other file or directory handle. You can mitigate some of that risk by localizing the symbol first, but that's pretty ugly. Since Perl 5.6, you can use an undefined scalar variable as a lexical reference to an anonymous file handle or directory handle. Alternatively, see the IO::Handle or IO::Dir modules for an object-oriented approach.

vibe check on what's ok and what's not:

opendir DH, $some_dir;            #not ok
opendir *DH, $some_dir;           #not ok
opendir \*DH, $some_dir;          #not ok
opendir local *DH, $some_dir;     #not ok
opendir $dh, $some_dir;           #ok
opendir my $dh, $some_dir;        #ok
opendir our $dh, $some_dir;       #ok
opendir local $dh, $some_dir;     #ok
my $dh = IO::Dir->new($some_dir); #ok

And Perl7 will probably drop support for bareword filehandles. 😭

πŸ’– CONFIGURATION πŸ’–

This Policy is not configurable except for the standard options. no cap.

πŸ¦‹ SEE ALSO πŸ¦‹

  • Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles::Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles
  • IO::Handle
  • IO::Dir

πŸ‘‘ AUTHOR πŸ‘‘

Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>, github.com/pali, github.com/raforg. iconic. 🫢

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


xoxo, the footer said: perl v5.42.0 Β· 2024-10-23 Β· Perl::...Handles(3pm) πŸ’–

πŸ“¦ where this page comes from