logo
Free, Micro AI Code Reviews That Run on Git Commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, Micro AI Code Reviews That Run on Git Commit | Product Hunt git-lrc - Free, Micro AI Code Reviews That Run on Git Commit | Product Hunt

Perl::Critic::Policy::Community::LexicalForeachIterator - Don't use undeclared foreach loop iterators

Affiliation

       This policy is part of Perl::Critic::Community.

Author

       Dan Book, "dbook@cpan.org"

Configuration

       This policy is not configurable except for the standard options.

Description

       It's possible to use a variable that's already been declared as the iterator for a foreach loop, but this
       will localize the variable to the loop and its value will be reverted after the loop is done.  Always
       declare the loop iterator in the lexical scope of the loop with "my".

        foreach $foo (...) {...}    # not ok
        for $bar (...) {...}        # not ok
        foreach my $foo (...) {...} # ok
        for my $bar (...) {...}     # ok

       This policy is a subclass of the Perl::Critic core policy
       Perl::Critic::Policy::Variables::RequireLexicalLoopIterators, and performs the same function but in the
       "community" theme.

Name

       Perl::Critic::Policy::Community::LexicalForeachIterator - Don't use undeclared foreach loop iterators

See Also

       Perl::Critic

perl v5.40.1                                       2025-03-22              Perl::Critic::...ForeachIterator(3pm)

See Also