While working out some bugs in ParTcl I came across something roughly
equivalent to the following Perl code (I'm using Perl because I
believe more people know Perl than Tcl, at least on this list):

  #!/usr/bin/perl
  $var   = "Foo";
  *alias = *var;
  $alias = undef;
  $alias = "Baz";
  print $var, "\n";

And I'm stuck wondering how I'm supposed to implement that in PIR. Or
at least what the best way is to implement that in PIR.

Currently, ParTcl works this way:

  $alias = undef

translates to

  null $P1
  $P2 = getinterp
  $P2 = $P2["lexpad"; 1]
  $P2['$alias'] = $P1

That is, we null variables when want them to appear almost as if
they'd never existed. (Almost because aliases still work.) Tcl is a
bit stricter than Perl, so any time we try to read an undefined value
we get an error. So

  print $alias

translates to something like

    $P0 = find_lexical '$alias'
    if null $P0 goto error
    "&print"($P0)
    end
  error:
    print "Undefined variable '$alias'"
    end

where the error will be given if $alias was never assigned a value or
if we assign undef to $alias (`$alias = undef`).

Normal assignment in Tcl looks like this:

  $P0 = find_lexical '$alias'
  assign $P0, new_value

We use `assign` to preserve any aliases (so that $foo == new_value, in
other words). However, if $alias is undefined (or `null`, in
PIR-speak), then that assignment fails with a "Null PMC access" error.

So what am I supposed to do? It appears that using `null` to mark
deleted/undefined variables won't work. But it's not clear to me that
using a Null PMC is a good idea (then we must perform `'isa` tests on
every read to see if that variable is undefined, which seems like it
would be expensive).

So what's the "correct" way to do this?

--
matt diephouse
http://matt.diephouse.com

Reply via email to