Perl::...dClose(3pm)

πŸ’… NAME

Okay so this page is Perl::...dClose(3pm) β€” the terminal squished it, it's literally Perl::Critic::Policy::InputOutput::RequireCheckedClose β€” and it's here to tell you: write my $error = close $fh; instead of just close $fh;. That's the whole vibe. ✨

🫢 AFFILIATION

This policy is part of the core Perl::Critic distribution β€” so if you have Perl::Critic, congrats bestie, you already have this one. No extra install arc required. πŸ’–

πŸ”₯ DESCRIPTION

So the perl builtin I/O function close returns a false value on failure. Like, it tells you when it didn't work. And that value should be checked, fr fr, so you actually know the close was successful and not silently falling apart. 🫠

my $error = close $filehandle;                   # ok
close $filehandle or die "unable to close: $!";  # ok
close $filehandle;                               # not ok

use autodie qw< :io >;
close $filehandle;                               # ok

See how the third one is just... sitting there, unbothered, not checking anything? Not ok. Do not be that line of code. 😭

You can use autodie, Fatal, or Fatal::Exception to get around this β€” they handle the checking for you, so the policy chills out. But heads up: currently, autodie is not properly treated as a pragma; its lexical effects aren't taken into account. So the vibe-check is imperfect, no cap. πŸ‘‘

πŸŽ€ CONFIGURATION

If you create a module that exports autodie, you can tell this policy about it with the autodie_modules setting β€” like this:

[InputOutput::RequireCheckedSyscalls]
autodie_modules = My::Thing

Yes, that's the key you want, bestie β€” autodie_modules. Go off. πŸ’…

🌸 AUTHOR

Andrew Moore <amoore@mooresystems.com> β€” iconic, we stan. πŸŽ€

πŸ¦‹ ACKNOWLEDGMENTS

This policy module is based heavily on policies written by Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>. Shoutout to the originator, absolute main character energy. πŸ’œ

Copyright (c) 2007-2011 Andrew Moore. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module. πŸ“œ


And that's the whole page, bestie β€” brought to you by perl v5.42.0, dated 2024-10-23, and by the letter C for CheckedClose. Go check your closes. πŸ“πŸ’–βœ¨

πŸ“¦ where this page comes from