Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with require

Status
Not open for further replies.

nook6

Programmer
Dec 18, 2002
24
0
0
GB
Hi

When i run these scripts i get this error

[Mon Jan 6 2003] index.pl: Undefined subroutine &main::vote called at index.pl line 6.
Compilation failed in require at index.pl line 4.


Any idea what might cause this i have been scratching my head for ages. (both scripts are in the same folder on the server)
If i put the vote sub in the index.pl script it works fine.

this is the main script named index.pl


#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

require "poll.pl";

&vote;


this script is named poll.pl


require "index.pl";

sub vote {
print "poll here";
my $poll1 = "poll.txt";
open(POLLFILE,&quot;<$poll1&quot;) || &error($!,&quot;cannot read $poll1.\n&quot;);
my @lines = <POLLFILE>;
close(POLLFILE);
my $poll = &quot;@lines&quot;;
print &quot;$poll&quot;;
}

1;


 
You're creating an infinite loop of requires since poll.pl &quot;requires&quot; index.pl which then &quot;requires&quot; poll.pl. Eventually, you'll either go on forever, or there will be some depth at which no more scripts can be required, at which time the subroutine goes undefined.

Either combine the scripts together, or else make one seperate from the other and only &quot;require&quot; one way.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
From perldocs on the subject of 'require':

&quot;demands that a library file be included if it hasn't already been included.&quot;

So there is no infinite loop. Requiring the index.pl is useless if that is the code that was executing to begin with.

I tested a similar setup: I had no problems.

#====== test1.pl =========

require 'test2.pl';

&test;

#==========================

#====== test2.pl ==========

sub test {
print &quot;it worked\n&quot;;
}

1;
#===========================

and it did work.

Do you have any &quot;package&quot; declarations in your &quot;poll.pl&quot; script?

--jim * * * Merry Christmas and Happy New Year! * * *

( Don't touch that Red Flag link you crazy freak, it's just a holiday greeting! )
 
Well, from the code given, that was my best guess. Coderifous, in your test2 program, try calling a subroutine in test1.

nook6, where is error() defined? Are you not including the full source code?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Hi

Yes there is an error sub and a few other subs, the scripts in thier entirety are quite big so i only put in the basics.
Everything is working fine except when i call the sub from the required script, i have tried with only requiring from one way ( from index.pl to poll.pl that didnt work either)
i have tried everything i can think of.

If i put the sub vote in the main index.pl it all works fine, but i need to move it into its own script so i can cut the size down and thats when i run into the problems.
 
Do you have a shebang line &quot;#!/usr/bin/perl&quot; in the poll.pl script? Try die'ing if require doesn't work (ie:
Code:
require &quot;poll.pl&quot; || die &quot;Could not load library: $!&quot;;
)
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Hi

Thanks for all your help im not sure what the problem was but i setup a config.pl and put the require in that and it worked.


Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top