Using use in programs vs modules

P

pgodfrin

Greetings,
I have little module, MyUtil.pm for example, that has utility
functions I use in most of my perl programs. I would like to use one
function from this module in another module - while at the same time
referring to the same module subroutine in the "calling" perl program.
For example

The module (pseudo-code):
package MyNewModule
use strict;
use warnings;
use Carp;
use lib '/home/modules';
use MyUtill qw(mytool);

sub test_it {
mytool();
}

The perl program (psuedo again):

#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use lib '/home/modules';
use MyUtill qw(mytool);
use MyNewModule qw(test_it);

print mytool();
print test_it();

This seems to work. I wonder though - is this bad form? Or does the
internal name space stuff keep it all straight?
thanks,
pg
 
G

Gunnar Hjalmarsson

pgodfrin said:
Greetings,
I have little module, MyUtil.pm for example, that has utility
functions I use in most of my perl programs. I would like to use one
function from this module in another module - while at the same time
referring to the same module subroutine in the "calling" perl program.
For example

The module (pseudo-code):
package MyNewModule
use strict;
use warnings;
use Carp;
use lib '/home/modules';
use MyUtill qw(mytool);

sub test_it {
mytool();
}

The perl program (psuedo again):

#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use lib '/home/modules';
use MyUtill qw(mytool);
use MyNewModule qw(test_it);

print mytool();
print test_it();

This seems to work. I wonder though - is this bad form? Or does the
internal name space stuff keep it all straight?

Seems ok to me. use()ing a module multiple times from different packages
does no harm; if the module is already loaded and hence added to %INC,
it won't be loaded again.

(In the real code I suppose you export stuff from your modules, or else
you wouldn't be able to import any symbols.)
 
T

Tad J McClellan

pgodfrin said:
I have little module, MyUtil.pm for example, that has utility
functions I use in most of my perl programs. I would like to use one
function from this module in another module - while at the same time
referring to the same module subroutine in the "calling" perl program.
For example

The module (pseudo-code):
package MyNewModule
use MyUtill qw(mytool);
^^
^^ oops
sub test_it {
mytool();


This is simply a shorthand way of writing:

MyNewModule::mytool(); # assuming MyUtil @ISA Exporter

which is also equivalent to:

MyUtil::mytool(); # no exporting needed

The perl program (psuedo again):


There is no psuedo-ness to it. It is Real Perl Code(tm).

use MyUtill qw(mytool);
print mytool();


This is simply a shorthand way of writing:

print main::mytool();

which is also equivalent to:

MyUtil::mytool();

This seems to work. I wonder though - is this bad form? Or does the
internal name space stuff keep it all straight?


The name space stuff keeps it all straight.
 
T

Tim Greer

pgodfrin said:
This seems to work. I wonder though - is this bad form? Or does the
internal name space stuff keep it all straight?

That depends, if the functions are unique enough and you know no one
else will be trying to figure out the code later, or that you won't
forget (though comments help a lot in that way). Still, it's a lot
easier when you use the right name spaces and know exactly what
function is from what module sometimes. Either way can work, but if
it's a large program that will end up with a lot of modules and
functions and you risk some similar or same function name, then you
have to start using name spaces to not cause conflicts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top