I want to validate a condition before doing the next step, but only raise a warning and skip the current value instead of die-ing.
How do I rewrite validate_me() subroutine without returning any value?
(Update) please note that the following code works as expected, it just that I want something else instead of returning 1 or 0 from validate_me() that still allow the code to behave the same.
foreach my $file ( @files ) {
validate_me($foo, $bar, $baz) or next;
do_something();
}
sub validate_me {
my ($foo, $bar) = @_;
if ( $foo > $bar ) {
carp("Something goes awol");
return 0;
}
if ( $bar != $baz ) {
carp("Odd");
return 0;
}
return 1; # success
}
Thanks.
Check the documentation for return. If you want to return nothing, don't give return an argument:
return;
First, there's no reason to use a label in your loop, a next;
by itself will automatically do the next iteration of the enclosing loop.
As to your question, I'm a little confused about what you're trying to accomplish. Your code as written will return zero, which is a false value, from validate_me
if any of your tests fail, and that should cause the next iteration of the loop. There's nothing in validate_me
that would cause anything to die.
Update:
I want something else instead of returning 1 or 0 from validate_me() that still allow the code to behave the same.
Is this what you mean? See eval
.
my @files = qw( a b c d e f );
FILES:
foreach my $file ( @files ) {
eval { validate_me($file) };
if (my $ex = $@) {
warn "'$file' failed validation: $ex";
next FILES;
}
print "$file passed\n";
}
sub validate_me {
my ($file) = @_;
die "Invalid filename\n" if 0.5 > rand;
return; # explicitly return nothing
}
Now, the sane thing to do would be to refactor validate_me
into its components:
for my $file ( @files ) {
if ( is_something_gone_awol($file, $mile) ) {
warn "Something has gone awol: '$file' and '$mile'\n";
}
elsif ( is_something_odd($file, $smile) ) {
warn "Something is odd: '$file' and '$smile'\n";
}
else {
do_something($file);
}
}
sub is_something_gone_awol {
my ($foo, $bar) = 0;
return $foo gt $bar;
}
sub is_something_odd {
my ($bar, $baz) = @_;
return $bar ne $baz;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With