#!/usr/bin/perl
# this run_dml() subroutine is hereby placed under public domain

sub run_dml($)
{
  my $tmp = "$ENV{HOME}/tmp";
  -d $tmp or $tmp = "/tmp";
  my $r = rand 1000000000;
  $tmp .= "/run_dml.$r";
  
  my @args = ("dml", "-o", $tmp, $_[0]);
  my $err = system @args;

  if ($err & 255) {
    unlink($tmp);
    return -1;
  }

  my $res = $err >> 8;

  %dml = ();

  open(DML_OUT, "< $tmp");
  while (<DML_OUT>) {
    chomp;
    /^res=(\d+)/ and $res = $1;
      
    if (/^([^=]+)="(.*)"$/) {
      my ($name, $value) = ($1, $2);
      my $x = "";
      while ($value =~ /([^\\]*)\\(.)(.*)/) {
        $x .= "$1$2";
	$value = $3;
      }
      $x .= $value;
      $dml{$name} = $x;
    }
  }
  close(DML_OUT);
  unlink($tmp);
  $dml{"res"} = $res;
  return $res;
}

run_dml( <<EOF
Foo: <input id=foo> <br>
<button caption="1" res=1> <button caption="2" res=2>
EOF
);

print "foo = $dml{foo}, res = $dml{res}\n";
