The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Perl::Critic::Policy::TryTiny::ProhibitExitingSubroutine - Ban next/last/return in Try::Tiny blocks

VERSION

version 0.002

DESCRIPTION

Take this code:

    use Try::Tiny;

    for my $item (@array) {
        try {
            next if $item == 2;
            # other code
        }
        catch {
            warn $_;
        };
        # other code
    }

The next statement will not go to the next iteration of the for-loop, rather, it will exit the try block, emitting a warning if warnings are enabled.

This is probably not what the developer had intended, so this policy prohibits it.

One way to fix this is to use labels:

    use Try::Tiny;

    ITEM:
    for my $item (@array) {
        try {
            if ($item == 2) {
                no warnings 'exiting';
                next ITEM;
            }
            # other code
        }
        catch {
            warn $_;
        };
        # other code
    }

CONFIGURATION

This Policy is not configurable except for the standard options.

KNOWN BUGS

This policy assumes that Try::Tiny is being used, and it doesn't run if it can't find it being imported.

AUTHOR

David D Lowe <flimm@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Lokku <cpan@lokku.com>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.