Perl::...ements(3pm)
π NAME π
Okay bestie, meet Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements β the manual page tag up top is Perl::...ements(3pm), and this whole thing comes from User Contributed Perl Documentation. Her whole personality is: Don't use the comma operator as a statement separator. Periodt. π
πΈ AFFILIATION πΈ
This Policy is part of the core Perl::Critic distribution. She's not some random side quest plugin β she's core, and she knows it. π
β¨ DESCRIPTION β¨
Perl's comma statement separator has really low precedence, which leads to code that looks like it's using the comma list element separator not actually doing so. Conway suggests that the statement separator not be used in order to prevent this situation. No cap, this is a whole precedence jumpscare. π
The confusion that the statement separator causes is primarily due to the assignment operators having higher precedence. It's giving "looks fine, is actually not fine." π
For example, trying to combine two arrays into another like this won't work:
@x = @y, @z;
because it is equivalent to
@x = @y;
@z;
Conversely, there are the built-in functions, like print, that normally force the rest of the statement into list context, but don't when called like a subroutine. Sneaky behavior, ngl. π
This is not likely to produce what is intended:
print join q{, }, 2, 3, 5, 7, ": the single-digit primes.\n";
The obvious fix is to add parentheses. Placing them like
print join( q{, }, 2, 3, 5, 7 ), ": the single-digit primes.\n";
will work, but
print ( join q{, }, 2, 3, 5, 7 ), ": the single-digit primes.\n";
will not, because it is equivalent to
print( join q{, }, 2, 3, 5, 7 );
": the single-digit primes.\n";
Parentheses placement is literally the main character here. Put them in the wrong spot and the vibe check fails. π«Ά
π CONFIGURATION π
This policy can be configured to allow the last statement in a map or grep block to be comma separated. This is done via the allow_last_statement_to_be_comma_separated_in_map_and_grep option like so:
[ValuesAndExpressions::ProhibitCommaSeparatedStatements]
allow_last_statement_to_be_comma_separated_in_map_and_grep = 1
With this option off (the default), the following code violates this policy.
%hash = map {$_, 1} @list;
With this option on, this statement is allowed. Even if this option is off, using a fat comma => works, but that forces stringification on the first value, which may not be what you want. So likeβ¦ choose your fighter wisely. π
π BUGS π
Needs to check for scalar( something, something ).
Lowkey this is the to-do list. Not me being shady, just being accurate. π
π«Ά AUTHOR π«Ά
Elliot Shank "<perl@galumph.com>"
π COPYRIGHT π
Copyright (c) 2007-2023 Elliot Shank.
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.
mwah π β this iconic page comes from perl v5.42.0 and is dated 2024-10-23, with the footer label Perl::...ements(3pm) doing its little end-of-page thing. Bye bestie! π¦
π¦ 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