Re: goto return ?



JupiterHost.Net am Montag, 27. November 2006 21:37:
What about doing this?

return if do_one_then_two($whatever);
...
sub do_one_then_two {
my $what = $_[0];
if ($what) {
one();
two();
return 1;
}
return 0;
}

Thanks, I'm not looking for how to handle a condition necessarily.

Dear JupiterHost.Net

I want to be able to:

log_error_and_return($error, @return) if $whatever;

That's what you want? And it should return @return?

instead of

if($whatever) {
log_error();
carp $error;
return @return;
}

basically I want to override return to log and carp first, every time
its called.

Ok, another try (I would not use it myself):

sub my_return {
my ($error, $return_data)=@_;

# do what boss wishes today

return $return_data;
}

# unconditional usage:
#
return my_return($error, $return_data);

# conditional usage:
#
return my_return($error, $return_data) if $whatever;
foo() unless returned_above();

========

I think you have to explicitly use the return. Otherwise, Tom Phoenix's notes
apply.

It's a really good thing to see 'return' at exactly the place where the code
does (or can) return to the caller.

========

I'm not sure if glueing together such different things as handling error
messages and returning application data is a good thing...

Do you now of the possibility to override $SIG{__WARN__}? This would allow to
keep recommended and usual coding style.

Dani
.



Relevant Pages

  • Re: goto return ?
    ... sub do_one_then_two { ... basically I want to override return to log and carp first, ... You could consider using the -P qualifier on Perl, which will stuff your Perl program ... that's also an unpleasant thing to do and is discouraged in the documentation. ...
    (perl.beginners)
  • Re: goto return ?
    ... Rob Dixon wrote: ... sub do_one_then_two { ... basically I want to override return to log and carp first, ... This will work but if log_error ever fails then it will not execute return. ...
    (perl.beginners)