Performance of chained methods.

Nick 'Sharkey' Moore sharkey at zoic.org
Thu Oct 31 19:05:50 CST 2002


On Wed, Oct 30, 2002 at 02:48:12PM +1100, Scott Penrose wrote:
> 
> 	my $ref =  
> SomeClass->FirstMethod($param1)->AnotherMethod($param2)->MoreMethods();
>
> My problem is that each of these can fail. In my particular case I  
> don't care which fail or why, is it ok therefore to do this

Gonzo Suggestion:

-----
package QueryException;
use strict;

sub AUTOLOAD {
    return shift;
}

sub new {
    return bless \@_;
}

'fnord';
-----
package SomeClass;

sub FirstMethod {
    if (do_something()->success) {
        return UsefulClass::new(useful_thingy);
    } else {
	return QueryException::new('FirstMethod failed! Black helicopters!');
    }
}
-----
my $ref=SomeClass->FirstMethod($param1)->AnotherMethod($param2)->MoreMethods();

carp "Oh, golly" if ref($ref) eq "QueryException";
-----

Well, something along those lines anyway.

-----sharks



More information about the Melbourne-pm mailing list