subroutines with XML

S

Sara

Hi all,
Thanks to the guidance provided for an earlier query I am progressing
into references and objects.

Now I want to associate every element of an XML file with a
subroutine. I searched CPAN and also read Perl-XML FAQ; XML::Dumper is
excellent and almost exactly what I wanted but it doesn't support
references to Perl subroutines. I'm new to using Perl with XML, though
I have used Perl and XML separately for quite some time. Is there any
easy way to associate XML elements with specific subroutines. I
thought of using symbolic references in haste then didn't do so
because I was reminded of the advice given by many in this community.
Is it necessary to have elements of XML declared as part of a data
structure in the program to make this association work?
Every bit of help is welcome.
 
A

Anno Siegel

Sara said:
Hi all,
Thanks to the guidance provided for an earlier query I am progressing
into references and objects.

Now I want to associate every element of an XML file with a
subroutine. I searched CPAN and also read Perl-XML FAQ; XML::Dumper is
excellent and almost exactly what I wanted but it doesn't support
references to Perl subroutines. I'm new to using Perl with XML, though
I have used Perl and XML separately for quite some time. Is there any
easy way to associate XML elements with specific subroutines. I
thought of using symbolic references in haste then didn't do so
because I was reminded of the advice given by many in this community.

The advice usually goes on "...use a hash instead".
Is it necessary to have elements of XML declared as part of a data
structure in the program to make this association work?

In one way or another, yes. You need some string that uniquely identifies
each XML element (whatever that is). If they are themselves strings, fine.
If they are objects, they will stringify to a unique string. If they are
compounds, use a reference, which will also be unique. Build a hash (a
dispatch-table) like this:

my %table = (
$el1 => sub {
# code here
},
$el2 => \ &named_sub,
# more entries
);

That shows two ways of entering a coderef in a hash, compiled in
place, or using a named sub compiled elsewhere.

Later, given an XML element $element, you can call the code like this:

my $result = $table{ $element}->( $arg1, ...);

Anno
 
S

Sara

Anno Siegel wrote in message news:
In one way or another, yes. You need some string that uniquely identifies
each XML element (whatever that is). If they are themselves strings, fine.
If they are objects, they will stringify to a unique string. If they are
compounds, use a reference, which will also be unique. Build a hash ...
...Later, given an XML element $element, you can call the code like this:
my $result = $table{ $element}->( $arg1, ...);
Anno

Thanks for that, it was really helpful. I am now more clear about how
to proceed. Actually I am transforming an XML file from a source DTD
to another DTD. I am storing this relation in an XML file, read by
XML::Dumper. The solution provided above solves my problem.

But in case, the source or destination DTD changes (not both at a
time), there shouldn't be any problem recreating the XML file storing
the mapping information. But shouldn't the subroutines be associated
with both DTDs (elements of the DTDs), so that the "link" is still
maintained if one DTD is changed?
Thanks again for all the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top