[Pdx-pm] the quest for return_if
Eric Wilhelm
scratchcomputing at gmail.com
Tue Jun 26 17:57:19 PDT 2007
# from benh
# on Tuesday 26 June 2007 05:17 pm:
>Ideally I would love to do this:
>
>sub return_if {
> my ($eval, $value) = @_;
> $value = $eval if !defined($value);
> {FROM_THE_POINT_WHERE_I_WAS_CALLED}->return $value if
> defined($eval); }
That's not exactly clear. Are you trying to return the value if it is
defined? That is, you're trying to get away from two-line things like:
my $val = answer($param);
return($val) if(defined($val));
?
Yeah, that could be tighter, but I don't recall ever being bothered by
it (then again, I'm not looking at ten pages of them, are you?)
The opposite is pretty concise:
return() unless(defined(my $val = answer($param)));
>[colin]humm... goto looks like I could be worth a stab.
Probably want to avoid that. Any goto besides gosub (goto &subname) is
typically asking for trouble and gosub doesn't really apply here.
Sounds like you're asking for a macro of some sort. Since this is not
C, maybe try looking at it from a different POV.
If it is a big list of methods without any intermediate code, you could
just do something like:
foreach my $method (@methods) {
my $val = $self->$method;
return($val) if(defined($val));
}
You can do that with functions too, but you have to turn off strict or
make @methods be a list of references to the functions (or anonymous
subs with the values curried (curried?.))
my @funcs = (
sub {this_one($foo, @_)},
sub {that_one($bar, @_)},
sub {the_next($baz, $bar, $foo, @_)},
);
foreach my $func (@funcs) {
my $ans = $func->(@extra_params);
return($ans) if(defined($ans));
}
Or use a source filter ;-)
--Eric
--
Entia non sunt multiplicanda praeter necessitatem.
--Occam's Razor
---------------------------------------------------
http://scratchcomputing.com
---------------------------------------------------
More information about the Pdx-pm-list
mailing list