I want to check for a condition, print a warning, and return from a subroutine with a single line of code. This works (and I think warn returns 1):
return warn "can't find file" if not -f $input_file;
Can I do this safely? Or is there a better way to go?
That's perfectly safe, but it requires looking at the source to determine what value is returned (true), and it leaves no readable option to control what value gets returned. This is rather important because you'd normally want to return nothing/undef/false in this situation, but you're currently returning true.
All of the following alternatives allow you to specify the value returned:
warn("can't find file"), return if !-f $input_file;
(warn "can't find file"), return if !-f $input_file;
if (!-f $input_file) { warn "can't find file"; return }
-f $input_file or warn("can't find file"), return;
-f $input_file or (warn "can't find file"), return;
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