1 Jun 2001 deven   » (Journeyer)

Well, I don't know what's going on, but the system isn't acknowledging any certification for me anymore despite the NINE certifications currently listed on my public page. :-(

I was going to reply to nmw's post in this article about LISP and interned strings, but I guess I'll have to "reply" here for now. He said "Try doing that in Python, or Perl, or Tcl. You just can't, [...]" Well, I wondered about that, so I thought about whether something of the sort could be done in Perl, my preferred HLL.

Here's what I came up with:

  my($sub, $space, $name) = $AUTOLOAD =~ /^.*:((?:(.*)_)?(.*))$/;
  $interned_value{$space}{$name} = int($interned_value{$space}{""}++)
    unless exists $interned_value{$space}{$name};
  eval "sub $sub { $interned_value{$space}{$name}; }";
  goto &$sub;

The idea is to reference undefined subroutines like &foo_bar in the program, and the AUTOLOAD routine would create a new subroutine returning an integer index to use for a scalar array. The final underscore (if any) in the name would determine the namespace; &foo_name and &bar_name could end up with the same value. (The idea is to avoid wasting entries in a scalar array; think of "foo" and "bar" as structure names.) Once defined, the subroutines would continue to return the same value, which is kind of like interned values in LISP.

I don't know if this is actually faster, though. The subroutine lookup may well be slower than a hash lookup would be. However, this could be VERY useful to conserve memory if many similar data structures are used; using scalar arrays with interned symbolic indexes would be more space efficient than using many hashes with string indexes. (Hopefully a later version of Perl may have a more natural and efficient way to do this sort of thing.)

This isn't a perfect solution, but it does show that Perl is more flexible than it may appear at first. Of course, since Perl is pretty damn fast to begin with, tricks like this usually aren't necessary to get decent performance. Even with its more natural interned symbols, I doubt LISP can approach Perl's performance in general...

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!