?
Solved

call a sub in another file

Posted on 2006-06-06
6
Medium Priority
?
336 Views
Last Modified: 2010-05-18
How can I call a sub in another file?

I tried:

do 'myotherfile.cgi';
&subinotherfile();

But I get an error:
Undefined subroutine &main::subinotherfile called at /usr/share/parse.cgi line 13.
0
Comment
Question by:lwinkenb
6 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 16844049
What is in myotherfile.cgi?
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 16844164
#!/usr/bin/perl

use Time::Local;

sub subinotherfile
{
    # some stuff in here
}

sub getDate
{
# ...
}

sub convertDate
{
# ...
}
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16845271
could be as simple as adding a require statement in the main script:

require 'myotherfile.cgi'; #assumes it's in the same folder as the main script
subinotherfile();

see "use" and "require" for more details.

http://perldoc.perl.org/functions/require.html
http://perldoc.perl.org/functions/use.html



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Accepted Solution

by:
GnarOlak earned 200 total points
ID: 16845510
Is that the complete contents of myotherfile.cgi?  I've tried to recreate this on several platforms and it works in each case.  The only time I got the error message you expressed was when I deleted the 'do...' line.  I'm wondering if there is anything in that file that is putting the subroutines into a package other than "main".

The error message says that the error is on line 13.  What else is in parse.cgi?

I used these files to test:

a.cgi:
#!/usr/bin/perl
do 'b.cgi';
&subinotherfile();

b.cgi:
#!/usr/bin/perl
use Time::Local;
sub subinotherfile {
      print "Here\n";
}

Can you please test with these?  If they also fail then there is something about your perl environment that is causing the problem.  If this test does work then there must be something in your scripts that is causing the problem.  Knowing where the problem truly lies will help solve it.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 16845635
You're right, I must have had something messed up.  I copied an older version of the cgi file, and it worked fine.
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16845794
from the perl documentation on the 'do' function:

Note that inclusion of library modules is better done with the use and require operators, which also do automatic error checking and raise an exception if there's a problem.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question