Question

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;

Was it helpful?

Solution

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});
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top