http://qs321.pair.com?node_id=613671

dana has asked for the wisdom of the Perl Monks concerning the following question:

Good day Monks.

I have a problem which is fairly complex to explain, however I'm hoping someone can help me find a solution.

I've been working with RSPerl (http://www.omegahat.org/RSPerl/) in order to call R functions from within Perl. If I run a basic program I get the expected results without error. An example of a basic script from the RSPerl website is listed below:

use R; # This tests the AUTOLOAD feature of the R package in Perl. # It will call the sum function directly from R::sum(). &R::initR("--silent"); $x = &R::sum(1,2,3); print "Sum = $x\n";
The problem happens when I turn my Perl code which uses RSPerl into a module and call it from a primary script. The relevant portion of the code is listed below:
# Calculate HWE goodness-of-fit p-value # Simply for readability of code passed to R @x = @{ $alleles1 }; @y = @{ $alleles2 }; @fail = qw( FL PF NTC ); # Create a locus for the data, passing to a string in Perl $loc = &R::callWithNames( "locus", { 'allele1', \@x, 'allele2', \@y, 'locus.alias', $site, 'miss.val', \@fail } ); # Calculate HWE for valid sites, otherwise print error $testForNA = &R::call( "is.na", $loc ); if ( &R::call( "all", $testForNA ) == 1 ) { $hwe = "-"; } else { $hwe = &R::call( "hwe", $loc ); $hwe = sprintf( "%.4f", $hwe->getEl("gof.pval") ); #&R::call("print", $hwe); } return ( $hwe );

If I call the module containing the RSPerl call once, the values are calculated correctly, however an error occurs when calling a 'DESTROY' function that exists in the RReferences.pm module of RSPerl (detailed debug below):

--- STDOUT DEBUG ---
my print statement:
Calculating Stats on site: MitoA10045G...

RSPerl debug statements:
Destroying R reference object `3'
Destroying R reference object `1'
Destroying R reference object `2'
Error: could not find function "DESTROY"
In addition: Warning messages:
1: '\.' is an unrecognized escape in a character string
2: unrecognized escape removed from "\.pm$"
Caught error in R::call()
--- END DEBUG ---

If I call my module containing the RSPerl call multiple times, once for each site in the data, the first site returns expected values, however the errors associated with 'DESTROY' appear to prevent the successful completion of the second site statistics and the program dies:

--- STDOUT DEBUG ---
my print statement:
Calculating Stats on site: MitoA10551G...

RSPerl debug statements:
Error in .Call("R_lazyLoadDBfetch", key, file, compressed, hook, PACKAGE = "base") :
recursive default argument reference
Error in .Call("R_lazyLoadDBfetch", key, file, compressed, hook, PACKAGE = "base") :
recursive default argument reference
Error: recursive default argument reference
Error: recursive default argument reference
Error: could not find function "locus"
Error in .Call("R_lazyLoadDBfetch", key, file, compressed, hook, PACKAGE = "base") :
recursive default argument reference
Caught error in R::call()
Error: could not find function "hwe"
Caught error in R::call()
Can't call method "getEl" on unblessed reference at /home/user/lib/calcHWE.pm line 125, <IN> line 290.
Error: could not find function "DESTROY"
Caught error in R::call()
Error: could not find function "DESTROY"
Caught error in R::call()
DESTROY created new reference to dead object 'calcHWE', <IN> line 290 during global destruction.
--- END DEBUG ---

There isn't anything wrong with the data from the second site. If I remove the first site and run it on the remaining sites I get the same failure on the second call to the module containing the RSPerl call.

I have written to the RSPerl author, currently waiting for a response. I worked with him last year when I first installed RSPerl and noticed the problem. He and I worked together on other problems, but he didn't have time to track this one at the time. To be honest it fell off my radar and his radar. I was making one call out to the module and all was working okay -- I was biding my time waiting for a new release. However as datasets got larger, the hardware limitations necessitated the need for multiple calls.

The final piece of information is the following, the DESTROY subroutine:

sub DESTROY { my $obj = $_[0]; print "Destroying R reference object `", $obj->{name}, "'\n"; R::deleteRReference($_[0]); 1; }
Please let me know if any information would make this problem more clear.

UPDATE --

Perl v5.8.8
RSPerl v0.91-2
R v2.4.1
OS Fedora Core 5

I've tested on a few different linux flavors and versions -- all to no avail. I haven't been able to test on Solaris yet as we are upgrading our systems.