Skip to content

op.c: don't warn on 'open my $fh, $file;'#24378

Open
mauke wants to merge 2 commits intoPerl:bleadfrom
mauke:fix-4186-hacky
Open

op.c: don't warn on 'open my $fh, $file;'#24378
mauke wants to merge 2 commits intoPerl:bleadfrom
mauke:fix-4186-hacky

Conversation

@mauke
Copy link
Copy Markdown
Contributor

@mauke mauke commented Apr 19, 2026

We have a hacky heuristic that complains about Missing parentheses around "my" list when it sees my $x, $y; or similar, assuming the programmer intended to declare both variables as local: my ($x, $y);.

However, this warning is inappropriate for open/opendir because open my $fh, $mode, $file or opendir my $dh, $dir can appear in perfectly correct code. Detecting this case is not easy because the warning is generated by inspecting the tokenizer state and manually reparsing parts of the input. This commit adds even more hackery and reparsing to avoid the inappropriate warning.

Fixes #4186.


  • This set of changes does not require a perldelta entry.

Comment thread op.c Outdated
@tonycoz tonycoz added the defer-next-dev This PR should not be merged yet, but await the next development cycle label Apr 21, 2026
@tonycoz
Copy link
Copy Markdown
Contributor

tonycoz commented Apr 22, 2026

Should socket(), accept() be similarly excluded?

$ perl -Wce 'my($domain, $type, $proto); socket my $sock, $domain, $type, $proto;'
Parentheses missing around "my" list at -e line 1.
-e syntax OK
$ perl -Wce 'my ($listen); accept my $newsock, $listen;'
Parentheses missing around "my" list at -e line 1.
-e syntax OK

I couldn't think of any others and I didn't find any more after a quick look through pp_sys.c.

mauke added 2 commits April 25, 2026 10:00
We have a hacky heuristic that complains about `Missing parentheses
around "my" list` when it sees `my $x, $y;` or similar, assuming the
programmer intended to declare both variables as local: `my ($x, $y);`.

However, this warning is inappropriate for open/opendir because `open my
$fh, $mode, $file` or `opendir my $dh, $dir` can appear in perfectly
correct code. Detecting this case is not easy because the warning is
generated by inspecting and the tokenizer state and manually reparsing
parts of the input. This commit adds even more hackery and reparsing to
avoid the inappropriate warning.

Fixes Perl#4186.
These functions autovivify filehandles, just like open and opendir.
@mauke
Copy link
Copy Markdown
Contributor Author

mauke commented Apr 25, 2026

Makes sense. I've added socket and accept to the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

defer-next-dev This PR should not be merged yet, but await the next development cycle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inappropriate warning for 'my' in 'open'

2 participants