# declare lexical variable *outside the scope of the eval* my $BusUrl; # trap potential error eval { $BusUrl = $contact->getElementsByTagName( "BusinessUrl" ); }; # check if tag retrieval failed if($@) { # assign an empty (but defined) string $BusUrl = ''; } $bus_url = $BusUrl->item( 0 )->getChildAtIndex( 0 )->getData();