[Omaha.pm] Tight XML::Twig code

Jay Hannah jay at jays.net
Thu Nov 29 13:25:41 PST 2007


Here are some shorter ways to do some XML::Twig stuff I stumbled into 
today...

j


Before:

   (@x) = $root->get_xpath(
      'RoomStays/RoomStay/TimeSpan'
   );
   foreach my $TimeSpanNode (@x)
   {
       $TimeSpanNode->delete;
   }

After:

   for ($root->get_xpath('RoomStays/RoomStay/TimeSpan')) {
      $_->delete;
   }



Or use map, if you're into that:
   map { $_->delete } $root->get_xpath('RoomStays/RoomStay/TimeSpan');



Before:

   my (@p) = $GuestCounts->get_xpath(
      'GuestCount'
   );
   foreach my $GuestCount (@p)
   {
      
After:

   foreach my $GuestCount ($GuestCounts->get_xpath('GuestCount'))
   {



Before:

   $BookArg= new XML::Twig::Elt( 'BookItArgument', '');   # create the 
element
   $BookArg->set_att(Name => 'affiliate');
   $BookArg->set_att(Value => 'Kayak.com');

After:
 
   $BookArg= XML::Twig::Elt->new( BookItArgument => {
      Name  => 'affiliate',
      Value => 'Kayak.com',
   });





More information about the Omaha-pm mailing list