subroutines with XML

Discussion in 'Perl Misc' started by Sara, Aug 27, 2004.

  1. Sara

    Sara Guest

    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.
     
    Sara, Aug 27, 2004
    #1
    1. Advertising

  2. Sara

    Anno Siegel Guest

    Sara <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Aug 27, 2004
    #2
    1. Advertising

  3. Sara

    Sara Guest

    Anno Siegel wrote in message news:
    > Sara <> wrote in comp.lang.perl.misc:
    > > Hi all,
    > > 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 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 ...
    > ...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.
     
    Sara, Aug 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ReaprZero

    References and subroutines

    ReaprZero, Dec 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    453
    Gunnar Hjalmarsson
    Dec 4, 2003
  2. MackS
    Replies:
    0
    Views:
    472
    MackS
    Mar 11, 2005
  3. tshad

    Global subroutines

    tshad, Jan 24, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    377
    IPGrunt
    Jan 26, 2005
  4. fd123456

    Global subroutines

    fd123456, Jan 25, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    403
    tshad
    Feb 4, 2005
  5. fd123456

    Global subroutines

    fd123456, Jan 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    400
    tshad
    Jan 28, 2005
Loading...

Share This Page