Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock - Do not "return" in "do" block
This policy is a policy in the Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock distribution.
Using return statement in do block causes unexpected behavior. A return returns from entire subroutine, not from do block.
return
do
sub foo { my ($x) = @_; my $y = do { return 2 if $x < 10; # not ok return 3 if $x < 100; # not ok 4; }; return $x * $y; } print foo(5); # prints 2, not 10;
If you want to do early-return, you should move the body of do block to a new subroutine and call it.
sub calc_y { my ($x) = @_; return 2 if $x < 10; return 3 if $x < 100; return 4; } sub foo { my ($x) = @_; my $y = calc_y($x); return $x * $y; } print foo(5); # prints 10
This Policy is not configurable except for the standard options.
Copyright (C) utgwkk.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
utgwkk <utagawakiki@gmail.com>
To install Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock
CPAN shell
perl -MCPAN -e shell install Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock
For more information on module installation, please visit the detailed CPAN module installation guide.