A little blue-sky, here...

> > And strictly 
> > speaking, its an ordered associative array right? It doesn't really 
need 
> > the full range of expression offered by $0{...} and $0[...]. All it 
> > needs is $0[1] for ordered lookups and $0["1"] for named lookups.
>
> Nope. The array aspect returns $1, $2, $3, etc.
> The hash aspect returns the named captures.

Seems to me this dual-aspectedness could be useful in representing the
sort of tree nodes one would get from parsing XML. The hash aspect
allows access to the (unordered) attributes and the array aspect allows
access to the (ordered) child nodes. With such a collection type built-in,
perhaps embedded XML could be treated as a quote-like construct, yielding
tree values instead of string or regexp values like the other quote-like
constructs...

  my $doc = <html>
    <head><title>Howdy!</title></head>
    <body><p>Hello, strange new world!</p></body>
  </html>;

Not only is this handy for XML, but it may be handy also for parsers
written in Perl.

Some cleverness could be applied to come up with a set of tree-operators.
Roughly,

  $doc = tgrep { ref $_ or not $_ =~ m/^\s*$/s } $doc;

would kill the whitespace-only leaf nodes in the above tree.

Perl already does great stuff with scalars and lists, but it would be nice
to see it do some new things with trees (natively).

One crude example would be an 'at' tree-path op (think XPath, I guess) --
although I expect there are superior formulations of this idea:

  my $msg = $doc at "/html/body/p"; # Returns first node.
  print "$msg\n"; # Prints "Hello, strange new world!\n"

Or, imagining some big HTML document already in $html, this:

  my @h = $html at "h1|h2|h3"; # Returns a list of nodes.
  foreach my $h (@h) {
    my ($level) = $h =~ /^h(\d)$/; # Stringizes to element name
    my $title = join " ", tgrep { not ref $_ } $h; # Flatten to list of 
strings
    print("  " x $level, $title, "\n");
  }

would print a heading summary with indentation.

Perl can already do some pretty LISPy things, so why not some TREPpy
things, too?


Regards,

-- Gregor

Reply via email to