in reply to Parse XML with Perl regex

The code you've written so far has a few problems, and it's taking you in the wrong direction -- away from using XML parsing methods and towards the much more troublesome and unreliable approach of using regex matches on XML data.

(Sure, regex matches on XML data seem "easier" at first, but in the long run, they aren't. Note that two XML files can differ drastically in line count and white space content, despite containing the exact same set of information. XML parsing handles this variation automatically, while line-oriented regexes tend to choke on it. Also, there are cases where the ordering of XML elements may vary, yet the data content would still be considered as "the same".)

Of course, if you are really, completely sure and confident that white-space / line-break patterns in your XML data will never change from the sample data you've shown, then a regex solution would probably suffice.

Since you don't have  use strict; you may have missed the fact that you are loading  @files with file names, but then using  @xml to run your "foreach" loop. You're also missing a semi-colon where you need one.

After you fix that, you'll want to declare the hash that will hold your data (do this before the foreach loop over the files), and then in the block that matches the "it_size" element, you assign the hash element. Here's how it probably should look (not tested):

#!/usr/bin/perl use strict; use Data::Dumper 'Dumper'; my @files = glob('/abc*/info.xml'); my %hash; my $info_name; foreach my $xmlname(@files) { open XML, $xmlname or die "Cannot open $xmlname for reading: $!\n" +; while(<XML>){ if ( /\<info_name\>/i ) { ($info_name) = (/<info_name>([^<])/i); } if ( /\<it_size\>/i ) { my ($it_size) = (/<it_size>([^<])/i); $hash{$info_name} = $it_size; } } } print Dumper( \%hash );
(updated code to remove residual use of "$line", and to add parens for regex assignments to work right)

Note that I've simplified the regexes a bit.

One last thing that would be worth checking: might there be any duplicate "info_name" values scattered within or across your set of XML files? If so, does it matter that some "it_size" values may be lost (over-written) in the process?