hackdaddy has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I am experimenting with tied hashes, XPath, the DB module, and XML tree data structures in Perl

I have a statement where $x is a reference to an object and a tied hash as below:
$x->{one}{two}{three};
I want to "autovivify" keys 'one', 'two', and 'three'. These keys represent XML tags. However, using the tie hash function calls like FETCH and STORE, I really want to create a data structure of hashes with an anonymous array sandwiched between each key. An anonymous hash in the anonymous array will hold the information about the "key". This gets crazy when I want to have tags of the same name as children of the same node, but I want to access them with my "Perl" syntax. (I will also want to maintain the them in the order created.)
$x->{one}{two}{three}; $x->{one}{two}{four};
Hopefully, you have followed me so far. Now I will get to the point.

My question really is: Why do I have to bend over backwards to create "code" in the Perl syntax? Why can't I suddenly break into a user-defined syntax that maps to my code and objects? For instance:
use MyClass::XML::XPath; use syntax 'http://www.blah.com/xpath/blah.blah', 'MyClass::XML::XPath +'; /html/head/title[0] = "Page Title"; /html/body/h1[0] = "Some text here"; /html/body/form/input[@type="text" and @name="input"]; /html/body/form/input[@type="submit" and @name="Submit"]; print /*; my $title = /html/head/title[0];
By using the concept of autovivification in tandem with the XPath syntax, I should be able to automatically create an XML tree and populate/access it by using my own user-defined syntax. (Also, it would be cool to have an implicit XML tree -- sort of like the variable $_).

With Perl everything seems possible. How might I go about creating a user-defined syntax that maps to a class module? It seems like the DB module might be the starting point to override Perl functionality.

I am certain this is possible with a clever hack. Any assistance is greatly appreciated. Thanks.

Replies are listed 'Best First'.
Re: User-Defined Syntax, XPath, Tied Hashes, and Autovivification
by Abigail-II (Bishop) on Feb 11, 2004 at 11:38 UTC
    I am certain this is possible with a clever hack.
    You could try source filters. Or, if you are patient, perl6. If you are younger than 25, there might be a working pre-release of perl6 before your retirement.

    Abigail

      Thanks for the suggestion. I am looking at the Filter::Simple and Filter::Util::Call modules for source filters. Can you point me to any good examples of source filters? Thanks.
Re: User-Defined Syntax, XPath, Tied Hashes, and Autovivification
by etcshadow (Priest) on Feb 11, 2004 at 16:40 UTC
    Also, instead of source-filters, you could just put the psuedo-code into an arbitrary string, mangle the string into perl, and then eval that mangled string. (Obviously, this is example is oversimplified, but it gets the point accross)
    my $code = q{ $x/html/head/title[0] = "Page Title"; $x/html/body/h1[0] = "Some text here"; }; $code =~ s|/(\w+)|->{$1}|g; eval $code; die $@ if $@;
    ------------ :Wq Not an editor command: Wq
      Well, except that a source filter doesn't do the evalling - it just hands the modified string back to the compiler, this is just what a source filter does, isn't?

      Abigail

        Pretty much... it's just a different way to skin the same cat. Also, depending on your mind-set, it may be a little more direct (or less direct =D).
        ------------ :Wq Not an editor command: Wq