in reply to Undefined subroutine error while using R within Perl

G'day rkeshvardoost,

You have some questionable code here. Consider these two adjacent lines:

package R; package R::startR;

Are you missing some code that should come after package R;? If not, why did you use package R; in the first place?

The use lib 'R'; line seems to be in the wrong place in your code. Without knowing your directory structure, I can't even say if it's correct (regardless of its position).

In this part of your code:

package R::startR; ... @EXPORT_OK = qw(startR ...

that refers to R::startR::startR. Was that what you meant? If so, you haven't declared it!

At this point, I'm starting to have to make many guesses as to your actual intent. The following is how I might have written something that may be close to what you want.

Firstly, I created a directory (pm_1152700_libs) and populated it like this:

$ cat pm_1152700_libs/R.pm package R; use Exporter 'import'; our @EXPORT_OK = qw{startR}; sub startR { print 'This is &R::startR' } 1;
$ cat pm_1152700_libs/R/startR.pm package R::startR; use Exporter 'import'; our @EXPORT_OK = qw{startR}; sub startR { print 'This is &R::startR::startR' } 1;

I then wrote this test script (but do see the notes I've written afterwards):

#!/usr/bin/env perl -l use strict; use warnings; use lib 'pm_1152700_libs'; use R qw{startR}; use R::startR qw{startR}; R::startR(); R::startR::startR();

which outputs

This is &R::startR This is &R::startR::startR

You've used startR in both module and subroutine names. This is highly error-prone and I'd recommend you think of better names.

I've repeated the use of startR in both module and subroutine names; however, note the following:

Finally, I strongly advise that you use the strict and warnings pragmata at the start of all your scripts.

— Ken

Replies are listed 'Best First'.
Re^2: Undefined subroutine error while using R within Perl
by rkeshvardoost (Novice) on Jan 13, 2016 at 23:46 UTC

    Thank you for your response Ken.

    To clarify: Since the original code had this error, I tried (almost blindly) anything I could find online to address the error. The original code is the following:

    sub pick_wavelets { `touch $SU_DIR/$file_1.picks`; `rm $SU_DIR/$file_1.picks`; `sugain agc=1 wagc=0.01 < $SU_DIR/$file_1.su | suxwigb mpicks=$SU_ +DIR/$file_1.picks`; } BEGIN { unshift @INC, "/usr/local/perl5.16.0/lib/site_perl/5.16.0/Statistics +/"; unshift @INC, "/usr/local/perl5.16.0/lib/site_perl/5.16.0/"; }; use R; sub setup_R { use lib "/usr/lib64/R/"; use lib "/usr/local/src/RSPerl/src/"; use R; use RReferences; &R::startR("--silent");

    I also tried to define the subroutine by replacing the last part of the code with the following

    sub setup_R { use lib "/usr/lib64/R/"; use lib "/usr/local/src/RSPerl/src/"; use R; use RReferences; use R qw{startR}; R::startR(); &R::startR("--silent");

    However, it still has the same problem. The reason I have not used strict at the beginning is that it makes my program to have more errors. Thank you very much again,

    Ru