Re: subroutines with XML

From: Anno Siegel (anno4000_at_lublin.zrz.tu-berlin.de)
Date: 08/27/04


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



Relevant Pages

  • Re: Iterate through words in a string
    ... instead of concatenating into a string?" ... I want the list of words to be the value part of a key/value hash and values ... (such as array references) ... are also scalars. ...
    (comp.lang.perl.misc)
  • Re: Getting file name from the file path error
    ... I checked one users tools - References and it appears that it ... It is part of a code module in an Excel ... end of statement with the As highlighted after the(stFullName As String). ... Sub GetFileNameAs String ...
    (microsoft.public.excel.programming)
  • Re: accessors
    ... mentioned that returning references from functions makes using ... boundaries, but if i did, "string const& nameconst" is functionally ... the same as "string nameconst", so i don't see why i'd have to change ... anything (but if i did, the change does not really affect client code, ...
    (comp.lang.cpp)
  • Re: Importing Excel
    ... Function LastInStr(strSearched As String, strSought As String) As Integer ... Didn't realize you haven't set references yet. ... > named Microsoft Excel and select the box. ...
    (comp.databases.ms-access)
  • Re: Is garbage collection here yet?
    ... I don't consider tcl to be a fully higher-level language. ... Tcl has so many other cool features and such a clean ... As others have answered, Tcl does do ref-counting GC of its values, which works fine for strings as they can't contain circular references and are stateless/immutable. ... Basically, it's hard to distinguish a reference from any other string, which makes it difficult to know when it is safe to delete something. ...
    (comp.lang.tcl)

Loading