Let's say I have the following Groovy code:
String name = child.getParent()?.getParent()?.getName();
Note that getParent()
may return null
, and in this case the code continues to work without null pointer exceptions being thrown.
Is there a way to do this clearly in one line in Perl 5.8? I am open to writing a generic helper method to accomplish this.
I'm running into situations where I have several nested objects and am having to do something like:
my $name = $child && $child->getParent && $child->getParent->getParent && $child->getParent->getParent->getName;
Yes this is in one line, but is fugly IMO.