$object = $object->method1 or return undef; $object = $object->method2 or return undef; $object = $object->method3 or return undef;
The method_chain (apart from the evil of polluting UNIVERSAL :)) idea will have the problem of params.
What if I want $object->method1('foo')->method2->('bar');For similar situations, I've solved this problem for most of my code with method naming. Any capitalized method guarantees to return the object (or die) and any lowercase one doesn't.
So this is safe.
$object->Parent->Metadata->Property('Foo');while this is not
$object->parent->metadata->property('Foo');For some classes, you just make one or the other, for some classes you might actually want to define both.
sub parent {
my $self = shift
... or return undef;
$self;
}sub Parent { shift->parent(@_) or die "Failed to find parent node" }This means that the code can make a judgment on how "safe" it needs to be on a case by case basis.
For things that should never fail and you would justifiably want to throw an error/exception, you use ucfirst version, for more "tentative" code you stick to the normal ones.
Adam
Matthew Simon Cavalletto wrote:
On Tuesday, November 23, 2004, at 05:40 PM, Terrence Brannon wrote:
It would be neat if there were some method-chaining syntax/module which
indicated "return false if your method call returns false":
$object-?->method1-?->method2-?->method3
It should be easy to add a UNIVERSAL::method_chain method that would do this.
Calling syntax might look like this:
$object->method_chain( qw/ method1 method2 method3 / );
push @status_options, 'hidden' if
GCt::glyph_attribute->method_chain(
'retrieve' => [
glyph_type => $glyph_type, attr_name => 'face_hideable'
],
'attr_value'
);
(Or import and call it as a function if you'd rather not pollute UNIVERSAL.)
-Simon
_______________________________________________ sw-design mailing list [EMAIL PROTECTED] http://metaperl.com/cgi-bin/mailman/listinfo/sw-design
_______________________________________________ sw-design mailing list [EMAIL PROTECTED] http://metaperl.com/cgi-bin/mailman/listinfo/sw-design
