Click to See Complete Forum and Search --> : require/use problem


rigadon
09-24-2004, 12:24 PM
I have a functions library 'functions.pl' which I 'require' in a script. This is so that I can use the function 'write_log' that is contained within the library.

I also 'use' a module 'SOAP_User' in my script. This module also 'require's the same 'functions.pl' library.

My code is as follows:

package SOAPAtlas;

use strict;
use SOAP_User;

require 'functions.pl';

my $err_file = "soap/err";
unless (&write_log($err_file, "Starting SOAP server"))
{ die "Unable to open server error file $err_file\n";
}

...

package SOAP_User;

use DBI;

use strict;
require 'functions.pl';

sub new
{ my $proto = shift;
my $class = ref($proto) || $proto;

my $self = {};

bless ($self, $class);

return $self;
}

...

I get the following error whenever I run this code:

Undefined subroutine &SOAPAtlas::write_log called at soap_server.pl line 30.

However, if I remove the "require 'functions.pl';" line from the SOAP_User module it all works fine, until the SOAP_User module attempts to call write_log when I get a run-time error.

This is presumably to do with require only loading a specific library once but how do I get around the problem?

Cheers

Trudge
09-25-2004, 04:35 AM
If you have 'Programming Perl' you will note that 'require' is not recommended for pulling in definitions unless you want to delay loading.

<quote>
Second, a 'require' happens at run time, so it may be too late to serve as a declaration in the file invoking the 'require'.
</quote>
'use' on the other hand happens at compile time. So, I suggest trying 'use' ing functions.pl and see what that does. I'd be curious to see if it works too!

rigadon
09-27-2004, 07:51 AM
Hi

I've replaced functions.pl with the similar Functions.pm and 'use'd it in my script. Everything seems to be working fine now (a few 'Functions::' prefixes floating around the place but I can live with that :)

Cheers