Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to "return warn" in Perl?

Tags:

perl

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?

like image 272
perreal Avatar asked Nov 21 '12 00:11

perreal


1 Answers

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;
like image 173
ikegami Avatar answered Oct 25 '22 00:10

ikegami