0

I have a XML file which contains the below section:

<cptasks>
 <copy file="file1.txt" to="folder1/folder21" />
 <copy file="file2.txt" to="folder1/folder33" />
 <copy file="file3.txt" to="folder1/folder4" />
 <copy file="file4.txt" to="folder1/folder1" />
</cptasks>

I need to parse only the file/to pairs in this particular section and run a function on them - i.e.

some_func_name(<file_value>,<to_value>)

How can I do that?

Thank you in advance;

Anirvan
  • 6,214
  • 5
  • 39
  • 53
John Stadt
  • 500
  • 7
  • 17
  • You need to try yourself first, then ask questions if you run into problems. "Write the code for me"-type questions are usually closed. – TLP Oct 13 '11 at 16:19
  • @TLP has a point, but sometimes an example is worth a thousand perldocs--especially if you have minimal experience. That said, I do recommend reading the perldoc (which, btw, also has some very good examples). – MisterEd Oct 13 '11 at 16:27
  • Did the answer given solve your problem? If so don't forget to accept it so others with the same problem know the solution worked (and to satisfy my neurotic need for approval :). – MisterEd Oct 19 '11 at 19:43

1 Answers1

2
use XML::Simple;
my $cptasks = XMLin(qq~
<cptasks>
<copy file="file1.txt" to="folder1/folder21" />
<copy file="file2.txt" to="folder1/folder33" />
<copy file="file3.txt" to="folder1/folder4" />
<copy file="file4.txt" to="folder1/folder1" />
</cptasks>
~
);


for my $copy (@{$cptasks->{copy}}) {
    some_func_name($copy->{file}, $copy->{to});
}
MisterEd
  • 1,725
  • 1
  • 14
  • 15