On 7/6/07, Ray <[EMAIL PROTECTED]> wrote:

I would like to create module of common sub routines and
corresponding global variables.

That's how most modules start; you're on the right track.

I'm trying to use "use " and/or "require " but it
looks like I have to do extra coding to make variables defined in my
module file to be accessible to the .pl files that are using the
module file.

That's the Exporter. Check out 'perldoc Exporter' for the full story.

 # The package name is the module name
 package Your::Module;
 use strict;
 use warnings;
 use Exporter;
 our @ISA = qw{ Exporter };
 our($global) = "any global variable";

 # List of default exports
 our @EXPORT = qw{ important_sub };
 # List of available non-default exports
 our @EXPORT_OK = qw{ sub1 sub2 $global };

 # Subroutines and documentation
 sub sub1 { ... }
 sub sub2 { ... }
 1; # final true value

Then, in some .pl file or main program,....

 use strict;
 use warnings;
 # Import only what we need:
 use Your::Module qw{ $global sub2 };

 if ($global) {
   print "The global is $global!\n";
   sub2("whoopee!");
 }

Can someone point me to some examples of documentation on
how I can do this (w/out having to specifically "export" each variable
for subroutine)?

Well, sorry; you do have to specifically "export" each item, but
merely by listing it in the @EXPORT or @EXPORT_OK arrays, so it
doesn't require customs clearance. :-)

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to