in reply to style for returning errors from subroutines

How about to return CODE-refs e.g.
sub makeclosure { my @x=(@_); return sub { return @x } } sub yoursubhere{ #do stuff return makeclosure(val1,val2); #on error return bless sub {undef or die},"errormessage";
yoursubhere is called without checking like &yoursubhere($a->(),$s->())->() or with check like
if(ref($return=&yoursubhere($a->(),$s->())ne"CODE"){ #error } &nextsub($return->())

Replies are listed 'Best First'.
Re: Re: style for returning errors from subroutines
by Boldra (Curate) on Jun 15, 2001 at 13:54 UTC
    This looks really interesting. I wish I could understand more of it!

    You lost me just after the closure (which I have read about in The Panther Book, but have never used). Am I right in thinking that the closure creates a place to store return values, and that if a closure is not returned, an error is? Or is the error in the closure? Where exactly? Or is that left up to me?

    Boldra

      The closure is a holder for a value. You can instead return an error. That is a subref (legally the same as a closure, but not holding a value) that on deref gives a default (including a possibility to leave the program for dead) but is blessed to contain the error and at the same time providing an easy test to distinguish from a closure.