[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Tight XML::Twig code



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',
  });