CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
module doesn't see functions in required file

 



swechsler
Novice

Aug 31, 2011, 9:55 AM

Post #1 of 7 (1335 views)
module doesn't see functions in required file Can't Post

When I call a function (in this case, dump_change_network_settings() ) in a package, it doesn't see the functions within a file that's been REQUIREd.

The module starts like this:

Code
package DBA::ChangeNetworkSettings; 

use strict;
use DBA::Stack;
use Data::Dumper;
require Exporter;
require "/dba/shared/bin/dba_subs.pl";

our @ISA = qw(Exporter);
our @EXPORT = qw(dump_change_network_settings);
[..]
sub dump_change_network_settings($$) {
# Change network timeout settings before/after dump
# option: 0 - reset, 1 - set
my ($mysql, $option) = @_;
my $dbh = $mysql->connect();
dba_debug(1, "In dump_change_network_settings()");


The dba_debug function is defined within dba_subs.pl:

Code
sub dba_debug { 
my ($debug_level, $string) = @_;
return unless my $debug = $ENV{DEBUG};
if ($debug >= $debug_level) {
print STDERR "$string\n";
}
}


dump_change_network_settings() is called by the main script. When I run it, I get the following error:

Undefined subroutine &DBA::ChangeNetworkSettings::dba_debug called at /dba/mysql/shared/lib/DBA/DBA/ChangeNetworkSettings.pm line 28.

Interestingly, dba_subs is REQUIREd by another module here (written by someone else) and it works fine. I've compared that module with mine and can't see what's being done differently.

Help!

Thanks!

Code



wickedxter
User

Aug 31, 2011, 10:01 AM

Post #2 of 7 (1332 views)
Re: [swechsler] module doesn't see functions in required file [In reply to] Can't Post

it b/c the module isnt being found at compile time... look in to the base module lib this should work..


swechsler
Novice

Aug 31, 2011, 10:06 AM

Post #3 of 7 (1331 views)
Re: [wickedxter] module doesn't see functions in required file [In reply to] Can't Post

I'm not following. The error occurs at runtime, not compile time.


wickedxter
User

Aug 31, 2011, 5:49 PM

Post #4 of 7 (1325 views)
Re: [swechsler] module doesn't see functions in required file [In reply to] Can't Post

when i had to require anouther perl file i also have to direct perl to look in the directory useing the " lib " module thats bundle with perl or you can use a begin statement and push the directory to the @INC array which holds all the directories perl will look for modules.

http://perldoc.perl.org/lib.html

the lib module does the work for you


Code
use strict; 
use lib('/dir/to/file');
require "perlfile.pl";

my $result = sub_from_perlfile($blah,$moo);



swechsler
Novice

Aug 31, 2011, 7:13 PM

Post #5 of 7 (1315 views)
Re: [wickedxter] module doesn't see functions in required file [In reply to] Can't Post

I understand what you're saying, but:

a) I specify the absolute path in the require statement, and
b) If that were the case, then I'd get the complaint at compile time, and the script clearly is running when i get this error.


wickedxter
User

Aug 31, 2011, 7:25 PM

Post #6 of 7 (1312 views)
Re: [swechsler] module doesn't see functions in required file [In reply to] Can't Post

Ok then to acces the sub from that script
dba_subs::dba_debug()


FishMonger
Veteran / Moderator

Aug 31, 2011, 8:51 PM

Post #7 of 7 (1303 views)
Re: [swechsler] module doesn't see functions in required file [In reply to] Can't Post

Using the lib pragma as wickedxter suggested is the proper way to include the directory where the required script is located.

A require statement occurs at runtime as opposed to the use statement which occurs at compile time.

Using prototypes is in 99% of the cases the wrong choice and even without seeing the rest of your code I can say with 100% assurance that it's the wrong choice in this case.

Your dba_subs.pl script should be converted to a true module and loaded via a use statement instead of a require statement.

Since a google search on DBA::Stack doesn't find anything useful, I'll assume that it's a custom module.

Based on the above, this is how I'd write your posted code.

Code
package DBA::ChangeNetworkSettings; 

use strict;
use warnings;
use lib "/dba/shared/bin/";
use DBA::Stack;
use DBA::Subs qw(:all); # this would replace your dba_subs.pl script
use Exporter;
use Data::Dumper;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw(dump_change_network_settings);

sub dump_change_network_settings {
# Change network timeout settings before/after dump
# option: 0 - reset, 1 - set
my ($mysql, $option) = @_;
my $dbh = $mysql->connect();
dba_debug(1, "In dump_change_network_settings()");
...
...
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives