This isn't very pretty, but first define a class:
package Undef; sub AUTOLOAD { undef; }
and then you can string at the cost of a few parens with:
$text = (( $elt->first_child( 'subelt') || 'Undef' )->first_child( 'subsubelt')||'Undef' )->text();
Don't like that? Well then you can just create a method to do the calls dynamically like this:
# Takes an array of anon arrays of method calls, and # chains them. Returns the first non-object of the end of # the call. sub chain_meth { my $obj = shift; my $last_call = pop; foreach my $call (@_) { my ($meth, @args) = @$call; $obj = $obj->$meth(@args); return $obj unless ref($obj); } # Make the last one respect context properly. my ($meth, @args) = @$last_call; return $obj->$meth(@args); }
and now you can:
$text = $elt->chain_meth( ["first_child", "subelt"], ["first_child", "subsubelt"], ["text"] );

In reply to Re (tilly) 1: Stringing method calls by tilly
in thread Stringing method calls by mirod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.