use XML::Twig; use strict; use warnings; my $twig = XML::Twig->new; my $students = $twig->parse(\*STDIN)->child('students'); my @people = $students->children('person'); foreach my $person_element (@people) { my $person = person_as_hash($person_element); print "$person->{'firstname'} $person->{'lastname'} <$person->{'email'}>\n"; } sub person_as_hash { my ($p) = @_; # Get the person's name my $name_element = $p->first_child('name'); my $first = $name_element->first_child('firstname')->text; my $last = $name_element->first_child('lastname')->text; # And e-mail address my $email_element = $p->first_child('email'); return { 'firstname' => $first, 'lastname' => $last, 'email' => $email_element->text, }; }