Re: subroutines with XML
From: Anno Siegel (anno4000_at_lublin.zrz.tu-berlin.de)
Date: 08/27/04
- Next message: Brendon Caligari: "Re: Xah Lee's Unixism"
- Previous message: Uri Guttman: "Re: Object Oriented programs, Perl and others"
- In reply to: Sara: "subroutines with XML"
- Next in thread: Sara: "Re: subroutines with XML"
- Reply: Sara: "Re: subroutines with XML"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 27 Aug 2004 18:35:11 GMT
Sara <sa_ravenone@yahoo.com> 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
- Next message: Brendon Caligari: "Re: Xah Lee's Unixism"
- Previous message: Uri Guttman: "Re: Object Oriented programs, Perl and others"
- In reply to: Sara: "subroutines with XML"
- Next in thread: Sara: "Re: subroutines with XML"
- Reply: Sara: "Re: subroutines with XML"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|