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

Hi all,

i need help in accessing the subroutines present in the perl scripts in perl modules.

getHosts() and getIP() subroutines are present in /tmp/utils/Utili.pl (perl script).

when i compile that master.pl, iam getting Undefined subroutine &Modules::test2::getIP called at /tmp/Modules/test2.pm

getIP() subroutine was present in the /tmp/utils/Utili.pl perl script.

i cannot take risk of changing /tmp/utils/Utili.pl to perl module, since it was used in hundreds of scripts and requires lot of testing effort.

could some one help me on this.?

please find the sudo code below.

Example:

master script: master.pl

in master.pl iam calling new subroutine in perl modules "test1.pm" and "test2.pm" as below.

use strict; use warnings; use lib "/tmp"; use Modules::test1; use Modules::test2; my $installation1 = Modules::test1->new(); my $installation2 = Modules::test2->new();

test1.pm:

in "test1.pm" i have the below code,

package Modules::test1; use strict; use warnings; require "/tmp/utils/Log.pl"; require "/tmp/utils/Utili.pl"; my @Hosts=getHosts(); sub new() { print "inside new\n"; }

test2.pm

in "test2.pm" i have the below code,

package Modules::test2; use strict; use warnings; my @IP=getIP(); require "/tmp/utils/Log.pl"; require "/tmp/utils/Utili.pl"; sub new() { print "inside new\n"; }

Replies are listed 'Best First'.
Re^2: Need help in accessing subroutines in perl scripts in perl modules.
by hippo (Archbishop) on Jun 16, 2016 at 12:37 UTC

    If, as reported, you are seeing the error with getIP() but not with getHosts() it is quite possible that getIP is not actually the correct name of the sub. Since we cannot see Utili.pl it's rather hard to diagnose beyond that. Is this the entirety of the error messages which are displayed?

    An SSCCE would help your cause considerably.

      Hi, getIP() is correct name, its not only this subroutine, whatever subroutines which were present in the /tmp/utils/Utili.pl perl script if i call in test2.pm iam getting the same error. the same subroutines are working if i call in test1.pm in both the modules, iam using the below lines to access the subroutines. require "/tmp/utils/Log.pl"; require "/tmp/utils/Utili.pl"; anything wrong iam doing here ???

Re: Need help in accessing subroutines in perl scripts in perl modules.
by stevieb (Canon) on Jun 17, 2016 at 12:42 UTC

    This post is a verbatim copy of my answer from StackOverflow, with the example slurped from one of my comments.

    require happens at runtime, so in this code:

    my @IP=getIP(); require "/tmp/utils/Log.pl"; require "/tmp/utils/Utili.pl";

    getIP() has not yet been loaded from the "Utili.pl" file, so it's looking in its own scope for that sub. Move things around so that the subs get loaded before you call them:

    require "/tmp/utils/Log.pl"; require "/tmp/utils/Utili.pl"; my @IP=getIP();

    Here's an example. require returns 1 on success:

    perl -E 'say 5; say require Data::Dumper' 5 1

    update: Correction: as choroba points out below, require returns whatever comes last in the module on first pass, then just returns 1 on subsequent calls.

      > require returns 1 on success:

      For the first time, require returns whatever comes last in the module. Next time for the same module, it returns just 1. See also Return values.

      $ perl -wE 'say require Syntax::Construct for 1, 2' Syntax::Construct 1

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Thanks for pointing that out, choroba!