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:
loading using 'use'

 



angshuman
New User

Jun 24, 2010, 11:33 AM

Post #1 of 5 (807 views)
loading using 'use' Can't Post

Why is the following script (abc.pl) giving an error message stating that

abc() is not accessible ?

However the scripts runs perfectly fine when i put 'use B' followed by 'use A' ?

Can anybody help ?????

abc.pl


#!/usr/bin/perl


use lib ".";
use A;
use B;


def();

########################################


A.pm


package A;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
abc
);


use B;


sub abc{
print "Inside sub abc of package A\n";
}

########################################


B.pm


package B;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
def
);
use A;


sub def{
print "Inside sub def of package B -> calling sub abc from package A\n";
abc();
}



BillKSmith
Veteran

Jun 24, 2010, 8:07 PM

Post #2 of 5 (792 views)
Re: [angshuman] loading using 'use' [In reply to] Can't Post

I cannot explain your problem, but I did notice that module A does not really use Module B. When I removed the use B statement from module A, everything worked as expected with the modules in either order.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Jun 25, 2010, 6:43 AM

Post #3 of 5 (773 views)
Re: [angshuman] loading using 'use' [In reply to] Can't Post

Please use the code tags when posting your code.

Unfortunately, I don't recall the "under the hood" details on how perl loads modules, but the order that you "use" them can/will alter how and when they are loaded/inherited.

abc.pl never calls the sub that is defined in A.pm, so there's no reason to "use" it in that script.

Likewise, A.pm doesn't call anything from B.pm, so there's no reason to for A.pm to "use B;".

All Perl scripts and modules should use the strict and warnings pragmas and all vars need to be declared with either the my or our keyword depending on its required scope.

Modules should always end with a true statement. i.e. the last line should be

Code
1;


Here's the updated/corrected version.

abc.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;
use lib ".";
use B;

def();


A.pm

Code
package A; 

use strict;
use warnings;
use Exporter;

our @ISA = qw( Exporter );
our @EXPORT = qw( &abc );

sub abc{
print "Inside sub abc of package A\n";
}

1;


B.pm

Code
package B; 

use strict;
use warnings;
use Exporter;
use A;

our @ISA = qw( Exporter );
our @EXPORT = qw( &def );

sub def{
print "Inside sub def of package B -> calling sub abc from package A\n";
abc();
}

1;



(This post was edited by FishMonger on Jun 25, 2010, 6:44 AM)


angshuman
New User

Jun 25, 2010, 7:16 AM

Post #4 of 5 (767 views)
Re: [FishMonger] loading using 'use' [In reply to] Can't Post

Hi All,
Thanks for all the quick replies...

What i had provided as code was really a very small form of what i was actually facing. So A.pm and B.pm are module which extensively use each others subroutines.
So it would be very helpful for me if somebody could explain how perl exactly loads in the modules. What I believed was that 'use' loads in the modules at compile time, so at execution time, everything gets resolved. But as i see that is not the case...

Anyways thanks for all who replied to help me out, and would like somebody to explain me a bit about how exactly perl reacts when it sees 'use'


kencl
User

Jul 3, 2010, 10:29 AM

Post #5 of 5 (671 views)
Re: [angshuman] loading using 'use' [In reply to] Can't Post

since "the scripts runs perfectly fine when i put 'use B' followed by 'use A' ?" is there any reason you can't just load them in that order and be done with it?

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!

 
 


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

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