Working around bugs

ethan on 2004-06-22T09:13:04

Doing that is something I absolutely hate, because it can make you look quite stupid. See this code:

sub validate { 
    my $o = shift;
    
    while (@_) {
	my ($key, $val) = splice @_, 0, 2;
	if ($key eq 'UNDEF') {
	    Inline::Lua->register_undef(\@$val), next if ref $val eq 'ARRAY';
	    Inline::Lua->register_undef(\%$val), next if ref $val eq 'HASH';
	    Inline::Lua->register_undef(\*$val), next if ref $val eq 'GLOB';
	    Inline::Lua->register_undef(\&$val), next if ref $val eq 'CODE';
	    Inline::Lua->register_undef($val);
	}
    }
}


Now, why do I possibly dereference $val just to pass a reference to the dereferenced value? register_undef is an XSUB. What it receives when just passing the raw reference is for some reason an SvPVIV:

SV = PVIV(0x82a5ab8) at 0x81b9134
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,ROK)
  IV = 0
  RV = 0x814a75c
  PV = 0x814a75c ""
  CUR = 0
  LEN = 0


Note that the ROK flag is still set.

It should be, of course, an SvRV. I am not yet sure who to blame. The above validate method is triggered by Inline so I suspect it might do funny things to its arguments. But this cannot be confirmed when looking at its code. Odd.

On a related note, Inline::Lua is done. 80% of the perldocs are there as well (and 0% of the tests, naturally). My ambitious plans layed out in my last journal entry all turned out to be feasible, even with less effort than I had expected. Perl and Lua can now happily exchange basic types, arrays, hashes, tables, filehandles and functions without getting confused. Tomorrow I'll possibly be able to release it to the CPAN.