
Perl allows for chained method calls, if each method returns an object:
$object->method1->method2->method3
This even applies when a method returns a different object:
$object->getNewObject->method1
In WebGUI, in the Shop, I found this inside Shop::CartItem:
$self->getSku->onRemoveFromCart($self);
This gets the Asset::Sku associated with this and calls the onRemoveFromCart method on it. And the line works really well, until you have the case where a user has added that Sku to their cart, and then the Sku gets deleted from the system. Then getSku will return undef, and you get a hard error about calling a method on an undefined value.
Fortunately, this is a pretty rare occurance. I found it because I wrote some bad clean-up code for a test. First, I purged a Sku that I'd been using for testing, then I called $cart->delete, which first empties the cart of items before deleting the cart.
And, there's an easy fix for it.
my $sku = $self->getSku;
$sku->onRemoveFromCart($self) if $sku;
First, put the new object in a temporary variable, then only call a method on it if it true, which any valid object would be.
I'm sure that this particular idiom is all over WebGUI. Be on the watch for it. If you find it, please report a bug so that a developer can look at it, or better still, write a test for the method where it's used, and submit a patch to get it fixed.
The moral of the story, don't chain method calls when calling methods on an object that is returned, without checking that you really got an object to chain.

Copyright 2010 perlDreamer Consulting | All Rights Reserved | Site Map | Graphic Design by Plain Black