basic module questions

N

nadsinoz

I have a large script that contains a lot of subroutines. I would like
to pull the subs out into a separate file for each sub. Each sub still
needs to be called from my main script, and have access to the global
variables of the main script:

CURRENT:
mainscript.pl
-------------------
my $var1;
my $var2;

&subA();
&subB();

sub A { print $var1 };
sub B { print $var2 };


REQUIRED:
mainscript.pl
-------------------
my $var1;
my $var2;

&packageA::subA();
&packageB::subB();


packageA.pm
--------------------
print $var1;



Question:

within packageA.pm, how can I use var1 from mainscript without having
to explicity pass it in to the sub?


Many thanks ....
 
A

Anno Siegel

nadsinoz said:
I have a large script that contains a lot of subroutines. I would like
to pull the subs out into a separate file for each sub. Each sub still
needs to be called from my main script, and have access to the global
variables of the main script:

One module per sub? Well, I guess if you want it that way you can have it.

But massive use of global variables is usually a design error. Try
to get rid of them.

If you must, put the global variables in another module and export them
from there. Let the individual modules and the main program all import
them. (Untested)

Anno
 
T

Tad McClellan

nadsinoz said:
Subject: basic module questions


Your question is more about variable scoping than about modules.

I have a large script that contains a lot of subroutines. I would like
to pull the subs out into a separate file for each sub. Each sub still
needs to be called from my main script, and have access to the global
variables of the main script:

CURRENT:
mainscript.pl
-------------------
my $var1;
my $var2;

&subA();
&subB();

sub A { print $var1 };
sub B { print $var2 };


REQUIRED:
mainscript.pl
-------------------
my $var1;
my $var2;

&packageA::subA();
&packageB::subB();


packageA.pm
--------------------
print $var1;



Question:

within packageA.pm, how can I use var1 from mainscript without having
to explicity pass it in to the sub?


You can't, because you declared it to be a lexical variable, and
lexicals never cross file boundaries.

So declare it as a package variable instead:

our $var1;

Then within packageA.pm use

$main::var1


See:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html
 
B

Brad Baxter

nadsinoz said:
I have a large script that contains a lot of subroutines. I would like
to pull the subs out into a separate file for each sub. Each sub still
needs to be called from my main script, and have access to the global
variables of the main script:

I'm not answering the question you asked, but I have a suggestion.
It sounds like your script needs refactoring if it has grown so large
that you feel the need to break it into parts. Rather than breaking
it at the subroutine level, I suggest that you break it into modules
based on logical functionality.

This will shorten your script, because it will (presumably) use the
modules rather than contain all of the subroutines' code. This will
also give you a wonderful opportunity to write documentation and
test suites for each module, working toward the end of a fully
documented and tested script.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top