Perl::...iterals(3pm)

πŸ’… Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals

okay bestie, so this is one of those User Contributed Perl Documentation pages, and it lives in section 3pm β€” and the policy name is a whole mouthful, we know, we love her for it anyway: Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals. She says: always use single quotes for literal strings. periodt.


✨ AFFILIATION ✨

This Policy is part of the core Perl::Critic distribution. it's not some random third-party girly, she ships with the main character πŸ’…


🌸 DESCRIPTION 🌸

so the whole vibe is: don't use double-quotes or qq// if your string doesn't require interpolation, okay? like, if there's nothing to interpolate, why are you being extra? πŸ™ˆ this saves the interpreter a bit of work, and β€” more importantly for us β€” it lets the reader know that you really did intend the string to be literal. it's giving clarity. it's giving intention. it's giving ✨communication✨.

print "foobar";     #not ok
print 'foobar';     #ok
print qq/foobar/;   #not ok
print q/foobar/;    #ok

print "$foobar";    #ok
print "foobar\n";   #ok
print qq/$foobar/;  #ok
print qq/foobar\n/; #ok

print qq{$foobar};  #preferred
print qq{foobar\n}; #preferred

see how the #ok ones are literally doing something (interpolating, escaping, existing)? and the #not ok ones are just… a string in a costume 🎭 also note the #preferred lines β€” when you do need interpolation, curly braces qq{...} are the moment. iconic.

oh, and! use of double-quotes might be reasonable if the string contains single quote (') characters:

print "it's me";    # ok, if configuration flag set

lowkey that's the plot twist of this whole page β€” it's configurable, keep reading 🫢


πŸŽ€ CONFIGURATION πŸŽ€

so you can tell this policy which quoting styles to exempt via the allow option. it must be a whitespace-delimited combination of some or all of the following styles: qq{}, qq(), qq[], and qq//. (yes, all four, no commas, just vibes and spaces ✨)

why would you even want this? because some folks have configured their editor to apply special syntax highlighting within certain styles of quotes β€” everyone's got their little setup, respect it πŸ’– for example, you can tweak vim to use SQL highlighting for everything that appears within qq{} or qq[] quotes. gorge. but if those strings are literal, Perl::Critic will complain 😭 so to prevent that, put the following in your .perlcriticrc file:

[ValuesAndExpressions::ProhibitInterpolationOfLiterals]
allow = qq{} qq[]

then there's the flag allow_if_string_contains_single_quote, which permits double-quoted strings if the string contains a single quote (') character. it defaults to off, so if you want it you gotta turn it on yourself, no cap. to turn it on, put the following in your .perlcriticrc file:

[ValuesAndExpressions::ProhibitInterpolationOfLiterals]
allow_if_string_contains_single_quote = 1

πŸ¦‹ SEE ALSO πŸ¦‹

  • Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars β€” yes she has a sister policy and yes they have opinions about each other πŸ’€

πŸ‘‘ AUTHOR πŸ‘‘

Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> β€” we stan a legend 🫢


Copyright (c) 2005-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. The full text of this license can be found in the LICENSE file included with this module.


okay that's the whole page, bestie, you ate that πŸ’… go forth and single-quote your literals, your future reader will thank you. mwah πŸŽ€

⭐ cute little sign-off: this was perl v5.42.0, dated 2024-10-23, filed under that 3pm section we mentioned at the top, and it's the Perl::Critic policy page for ProhibitInterpolationOfLiterals β€” iykyk 🌸

πŸ“¦ where this page comes from