Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- New to creating modules
- Nick Wedd
June 14, 2011, 12:07 pm
rate this thread
them together as at present. I have tried to understand what I read
about modules, but there are some gaps (in particular, the concepts
"file", "module", and "package" seem to overlap in a way that confuses
me). I would therefore appreciate answers to a few questions.
(1.) Does a package automatically end at the end of a file?
(2.) Can packages nest?
(3.) I like to begin my Perl files with
but if the next line is
@EXPORT = ( foo );
I get an error message. I can remove the
and get code that works, but there must be a better answer. What is it?
(4.) I can export functions from a module to a program, but how should
I export a variable? I have found three ways:
(a.) Declare it before the 'use strict' line
(b.) Use 'our'
(c.) Don't export the variable, export a little function that returns
None of these seems ideal - so what is the recommended method?
Re: New to creating modules
On Tue, 14 Jun 2011 13:07:34 +0100
The 'package' keyword affects the current package until the end of the
compilation unit. A common idiom is this:
Packages must end in a truthy statement to be loadable by require or
For more information on 'package', read its documentation here:
Yes, but beware: this following code won't likely work as you think:
package SubPackage; # this is the package SubPackage, not
In order to create Package::SubPackage, you need to do this:
our @EXPORT = qw(foo);
Exporting variables is pretty uncommon, but if you're going to export
one, you'll need to declare it in package scope; ie. with 'our'.
You might want to take a look at http://perldoc.perl.org/perlmod.html ;
it could clear up some of your confusion!
- » installation of perl GD with cpan on macosx snow leopard
- — Next thread in » PERL Modules Announcements