[Pdx-pm] the quest for return_if

Austin Schutz tex at off.org
Tue Jun 26 21:28:46 PDT 2007

On Tue, Jun 26, 2007 at 05:57:19PM -0700, Eric Wilhelm wrote:
> # 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)));

	I believe the intent is to do something like

assert( test() ); 

	and be able to have the return portion automatic. If you understand
what is meant it is far more concise. But the language doesn't cleanly
support it- which may be a boon, since it would be surprising behavior.  I had
at one point had the same wish, but something that fundamental to the
operation of the interpreter would be very hard to bolt on cleanly. Well,
unless you are a lot more clever than I am.

	If true/false will work for you, you can distill it to a simple test

	assert( test() ) or return;

	If testing defined():

	defined(assert(test())) or return;

	Basically all variations on the theme of Eric's response.


More information about the Pdx-pm-list mailing list