Perl::...rators(3pm)
π
Perl::...rators(3pm) π
β¨ User Contributed Perl Documentation, but make it group-chat β¨
πΈ NAME πΈ
bestie, this policy is named Perl::Critic::Policy::InputOutput::ProhibitBacktickOperators, and it's here to discourage stuff like this little menace:
@files = `ls $directory`
periodt π
π«Ά AFFILIATION π«Ά
This Policy is part of the core Perl::Critic distribution. slay.
β¨ DESCRIPTION β¨
okay so backticks are super-convenient, especially for CGI programs, but they lowkey make a lot of noise π β when they fail, they fill up STDERR with messages. The author thinks it's better to use IPC::Open3 to trap all the output and let the application decide what to do with it.
use IPC::Open3 'open3';
$SIG{CHLD} = 'IGNORE';
@output = `some_command`; #not ok
my ($writer, $reader, $err);
open3($writer, $reader, $err, 'some_command'); #ok;
@output = <$reader>; #Output here
@errors = <$err>; #Errors here, instead of the console
π CONFIGURATION π
Alternatively, if you do actually want to use backticks, you can restrict checks to void contexts by adding the following to your .perlcriticrc file:
[InputOutput::ProhibitBacktickOperators]
only_in_void_context = 1
The purpose of backticks is to capture the output of an external command. Use of them in a void context is likely a bug π. If the output isn't actually required, system() should be used. Otherwise assign the result to a variable.
`some_command`; #not ok
$output = `some_command`; #ok
@output = `some_command`; #ok
π¦ NOTES π¦
- This policy also prohibits the generalized form of backticks seen as
qx{}. - See
perlipcfor more discussion on usingwait()instead of$SIG{CHLD} = 'IGNORE'. - You might consider using the
capture()function from theIPC::System::Simplemodule for a safer way of doing what backticks do, especially on Windows. The module also has a safe wrapper aroundsystem().
π AUTHOR π
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> π
π COPYRIGHT π
Copyright (c) 2005-2011 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. The full text of this license can be found in the LICENSE file included with this module.
bye bestie π«Ά this page was brought to you by perl v5.42.0, dated 2024-10-23 β¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms