#!/usr/bin/perl use strict; use Data::Dumper; my %hash; my $self = \%hash; while () { if ( m{<(\w+)>([^<]+)} ) { # a content element $$self{$1} = $2; # store the content keyed by tag name } elsif ( m{<(\w+) ([^>]+)/>} ) { # an "empty" element my ($elem,$attr) = ($1,$2); # get tag name and attributes my %attrhash; while ( $attr =~ s/(\w+)="([^"]+)"// ) { $attrhash{$1} = $2; } push @{$$self{$elem}}, \%attrhash; # expect multiple instances } } print Dumper( $self ); # here's one way to access the structure's contents: for my $tag ( sort keys %$self ) { if ( ref( $$self{$tag} ) eq 'ARRAY' ) { for ( @{$$self{$tag}} ) { # $_ is now an array element containing a hash ref for my $attr ( sort keys %{$_} ) { print "$tag : $attr = $$_{$attr}\n"; } } } else { # the tag had a plain string as content print "$tag = $$self{$tag}\n"; } } __DATA__ hello gender