Skip to content

Avoid using $_ when loading the commands from namespaces #2181

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bbrtj
Copy link

@bbrtj bbrtj commented May 17, 2024

Summary

Solution proposed in #2175

Motivation

Polluting $_ can happen deep down in dependencies tree of the loaded command.

References

resolves #2175

@bbrtj bbrtj force-pushed the main branch 2 times, most recently from f6c8a1f to 91c03a9 Compare May 17, 2024 19:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the command-loading loop to avoid using the implicit $_, preventing unintended global variable pollution.

  • Replaced a chained grep/for using $_ with an explicit for my $pkg loop.
  • Added an early next unless _command($pkg) guard.
  • Updated the hash assignment to use the named $pkg variable.
Comments suppressed due to low confidence (3)

lib/Mojolicious/Commands.pm:68

  • [nitpick] Consider renaming $pkg to a more descriptive name like $module or $command_class to clarify that it represents a command class.
for my $pkg (find_modules($ns), find_packages($ns)) {

lib/Mojolicious/Commands.pm:67

  • [nitpick] Add a comment explaining that this loop avoids using $_ to prevent pollution from upstream code, clarifying the purpose of the refactoring.
for my $ns (@{$self->namespaces}) {

lib/Mojolicious/Commands.pm:71

}

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.

Commands error when topic variable set to undef in compilation
1 participant