Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/utgwkk/perl-critic-policy-controlstructures-prohibitreturnindoblock
https://github.com/utgwkk/perl-critic-policy-controlstructures-prohibitreturnindoblock
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/utgwkk/perl-critic-policy-controlstructures-prohibitreturnindoblock
- Owner: utgwkk
- License: other
- Created: 2020-11-12T07:14:49.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2020-11-16T02:12:28.000Z (almost 4 years ago)
- Last Synced: 2024-04-20T08:59:57.689Z (7 months ago)
- Language: Perl
- Size: 27.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
# NAME
Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock - Do not "return" in "do" block
# AFFILIATION
This policy is a policy in the [Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock](https://metacpan.org/pod/Perl%3A%3ACritic%3A%3APolicy%3A%3AControlStructures%3A%3AProhibitReturnInDoBlock) distribution.
# DESCRIPTION
Using `return` statement in `do` block causes unexpected behavior. A `return` returns from entire subroutine, not from `do` block.
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# CONFIGURATION
This Policy is not configurable except for the standard options.
# LICENSE
Copyright (C) utgwkk.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.# AUTHOR
utgwkk