Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

require issue.

by ant (Scribe)
on Mar 02, 2007 at 11:27 UTC ( #602860=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks

I'm doing some thing wrong here and for the life of me I can't see what's going wrong. All help gratefully recieved.

I have inherited some Perl scripts, and I have one Perl script program which has a require statement of another Perl file.
I also call a Perl Module that I've created.

Thus my main script requires 'somefile.pl' and uses newform.pm.

my main script then calls a sub routine sub_a in somefile.pl. (this works fine).

I then added to main script more code to call new->newform(this works fine).

However

if in the newform package I want to use another sub routine from 'somefile.pl' i declare require('somefile.pl')

and in the newform package I then call sub_b sub routine from 'somefile.pl'.

BUT This now falls over in the main script saying 'undefined subroutine &main::sub_a at blah'!

Can some one explain what's going on here and a possible solution, as my brain has now gone 'Kaput'.

Thanks in advance(hope that was explained clearly enough?)

Cheers

Replies are listed 'Best First'.
Re: require issue.
by davorg (Chancellor) on Mar 02, 2007 at 11:51 UTC
      Hi .. I tried to resolve and have the sample programs.. "Ant" correct me if I am wrong
      main.pl
      require "somefile.pl"; use newform; &someFile(); &newform::func1();
      somefile.pl
      sub someFile { print "LAMP"; } sub someFile1 { print "J2EE"; } 1;
      newform.pm
      package newform; require "somefile.pl"; sub func1 { &someFile1(); print "Inside func1"; } 1;
      while trying to run main.pl, getting the following error
      Undefined subroutine &main::someFile called at s1.pl line 5.
Re: require issue.
by osunderdog (Deacon) on Mar 02, 2007 at 12:56 UTC

    I ran through this with the debugger:

    $perl -d main.pl Loading DB routines from perl5db.pl version 1.25 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(main.pl:4): require "somefile.pl"; DB<1> S newform* newform::func1 newform::someFile newform::someFile1 DB<2> S main* main::BEGIN DB<3>

    So the functions you are requiring are getting included in the package.. because that's where you require them.

    One thing that require does is "check for redundant loading, skipping already loaded files." So in this case, the required script is getting included in the package. The package is compiled first. The second time that require is called is at the main, however this is after the package has been compiled. So at that time, the require fails/does not include the package a second time.

    So I changed the main code to scope the function call:

    use strict; require "somefile.pl"; use newform; &newform::someFile(); &newform::func1();

    and it works as expected.

    Alternatively you could do this in your main:
    sub BEGIN { require "somefile.pl"; }

    Which would force the require before the newform package, however you would have to also change the call to someFile in the newform package:

    &main::someFile();

    which might not make sense.

    Hazah! I'm Employed!

      After posting that request for help I continued looking for a solution and came up with this.

      in newform.pm I commented out the require statement
      package newform; #require "somefile.pl"; sub func1 { main::someFile1(); print "Inside func1"; } 1;
      I left the Requires('somefile.pl') in the Main.pl, Then, when in the newform.pm and I wanted to use the someFile1 function I called main::someFile1(); This seemed to work!

      Thanks all for looking into this also.
        Or put a package main; at the beginning of somefile.pl; that way each other file that uses something from somefile.pl makes sure that it's loaded, but always knows what package to expect it to be loaded into.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://602860]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-02-06 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?