Skip to content

Commit

Permalink
Replace while-each with foreach
Browse files Browse the repository at this point in the history
`each` sort-of remembers the last element it iterated over which can lead to unexpected results.
While it can used correctly, it was decided to amend our coding style use prefer foreach.

Fixes: #109
  • Loading branch information
jrha committed Sep 6, 2024
1 parent f50b380 commit 8010334
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/perl/ReporterMany.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ sub _rep_setup
# initialise with current "global" CAF::Reporter settings
# if DEBUGLV is not set
if (! exists($self->{$DEBUGLV})) {
while (my ($opt, $val) = each (%$CAF::Reporter::_REP_SETUP)) {
$self->{$opt} = $val;
foreach my $opt (keys %{$CAF::Reporter::_REP_SETUP}) {
$self->{$opt} = ${$CAF::Reporter::_REP_SETUP}{$opt};
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/perl/TextRender.pm
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ sub _initialize
STRICT => $DEFAULT_TT_STRICT,
RECURSION => $DEFAULT_TT_RECURSION,
};
while (my ($key, $value) = each %{$opts{ttoptions}}) {
$self->{ttoptions}->{$key} = $value;
}
foreach my $key (keys %{$opts{ttoptions}}) {
$self->{ttoptions}->{$key} = $opts{ttoptions}{$key};
};

# set render method
($self->{method}, $self->{method_is_tt}) = $self->select_module_method();
Expand Down Expand Up @@ -520,7 +520,8 @@ sub render_tiny

my $c = Config::Tiny->new();

while (my ($k, $v) = each(%{$self->{contents}})) {
foreach my $k (keys %{$self->{contents}}) {
my $v = $self->{contents}{$k};
if (ref($v)) {
$c->{$k} = $v;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/test/perl/exception.t
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ my $opts = {};
my $success_func = sub {
my ($arg1, $arg2, %opts) = @_;
push(@$args, $arg1, $arg2);
while (my ($k, $v) = each %opts) {
$opts->{$k} = $v;
foreach my $k (keys %opts) {
$opts->{$k} = $opts{$k};
};
return 100;
};
Expand Down

0 comments on commit 8010334

Please sign in to comment.