Skip to content

add a new warning against using \ in qw() #23403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: blead
Choose a base branch
from
Draft

Conversation

book
Copy link
Contributor

@book book commented Jul 5, 2025

I've seen AI-generated code try to use qw() to create lists containing strings with embedded whitespace using qw and \ to "protect" the whitespace. Things like:

my @list = qw(
    foo
    bar\ baz
);

Just like occurences of ',' and '#', I believe this should warn.

Note that the warning will only be emitted when the \ is followed by actual whitespace, so code like the following (from lib/App/Cpan.pm) will not warn:

my $epic_fail_words = join '|',
        qw( Error stop(?:ping)? problems force not unsupported
                fail(?:ed)? Cannot\s+install );
  • This set of changes requires a perldelta entry, and it is not included yet.

I've seen AI-generated code try to use qw() to create lists containing
strings with embedded whitespace using qw and \ to "protect" the
whitespace. Things like:

    my @list = qw(
        foo
        bar\ baz
    );

Just like occurences of ',' and '#', I believe this should warn.

Note that the warning will only be emitted when the \ is followed by
actual whitespace, so code like the following (from lib/App/Cpan.pm)
will not warn:

    my $epic_fail_words = join '|',
            qw( Error stop(?:ping)? problems force not unsupported
                    fail(?:ed)? Cannot\s+install );
@book book marked this pull request as draft July 5, 2025 01:39
Copy link
Contributor

@jkeenan jkeenan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you ever seen this backslash-to-escape-whitespace-in-qw in code written by a human?

If not, then maybe we should not apply this p.r. so that we have a way to readily identify AI-generated code.


(W qw) qw() lists contain items separated by whitespace; contrary to
what some might expect, backslash characters cannot be used to "protect"
whitespace from being split, but are instead treated at literal data.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want treated as rather than treated at?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a typo. Thanks.

@Grinnz
Copy link
Contributor

Grinnz commented Jul 5, 2025

This is adding a warning that the code will not do what was expected, which would not affect "identifying" the code. It seems a reasonable indication to me.

@book
Copy link
Contributor Author

book commented Jul 5, 2025

I saw this in AI-generated code, but it looked plausible enough that I had to double-check. ("What?! I didn't know you could do that... Oh. It turns out you can't." "You are absolutely right!")

Given the ubiquity of backlash as an escape character, it's reasonable to think that someone not fluent in Perl would try it.

The example in the description is what actually led me to show the warning only when immediately followed by whitespace.

@guest20
Copy link

guest20 commented Jul 5, 2025

This sounds like this warning will punishing people who actually write code and run it in production just to make it slightly more convenient for people who do not write code to copy/paste generated nonsense. Is that valuable?

Nobody "fixed the language" when the markov bot on IRC produced code that almost looks right. And if that is useful, I think that at absolute minimum the request should come from an actual person who's had this problem. I don't think perl should pro-actively change in response to "umm, i saw a screen shot of a tweet on reddit that got cross-posted to my telegram group where chatGPT was wrong about programming".

Nobody reported this bug. Nobody ran this code. Nobody even wrote this code. I think the same person should fix it.

@book
Copy link
Contributor Author

book commented Jul 5, 2025

Who uses qw to create strings that end with \, and will be "punished" by this warning?

I think it's going to be the same person who reported the bug, ran and wrote such code. (Nobody.)

To be honest, I'm not surprised nobody reported the absence of a warning. The bug is not the absence of a warning, it's the expectation that \ will DWIM. And the code that was fixed was in that qw expression. Nothing to report, move along.

However, in my first encounter with AI-generated Perl code in a realistic Perl project, the AI spit that out.

I think this kind of "you're holding it wrong" warning will only show up for someone (or something 🤖) who makes that very understandable mistake of assuming you can protect whitespace with backlashes. And that is exactly the kind of helpful warning Perl has been dispensing for a very long time.

It could be argued that allowing \ (backslash-space) in qw would be the more valuable fix. We've spent almost 40 years without it, so that ship has definitely sailed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants