Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Writing Modules

by perl.j (Pilgrim)
on Aug 23, 2011 at 02:54 UTC ( #921811=perlquestion: print w/replies, xml ) Need Help??
perl.j has asked for the wisdom of the Perl Monks concerning the following question:

I am writing my first module just to see if I can get it to work. It is extremely simple:

use strict; use warnings; package Example; use Exporter; @EXPORT = "$thing"; our $thing = "thing"; 1;

Then I used the module in a simpler script:

use strict; use warnings; use Mine::Package; print $thing;

I know I got the module name right. My module is located in Perl/site/lib/Mine/Example/ on my computer. So why isn't my program printing "thing"?


UPDATE:I changed some package and folder names to make it easier on everyone. I also changed my code to look like my reply to davido. Added strictures.


Replies are listed 'Best First'.
Re: Writing Modules
by davido (Archbishop) on Aug 23, 2011 at 03:22 UTC

    I know I got the module name right.

    Which module name? Your package is named here:

    package pack;

    You're invoking it with:

    use mine::package;

    When you get your package's name as declared with package to match the name as requested via use, and into a path within your Perl's /lib/* hierarchy that matches the name, you will find it works better.

    You wouldn't say "my $this = 5;", and then "say $that;". It's a similar principle.

    The next step will be in either using the fully qualified name of $thing, or exporting the variable into your caller's name-space. See perlmod, and Exporter.

    Also, by general convention people usually give their modules names where the first character of each word is capitalized, as in Quantum::Superpositions, or List::Util. (but not always...). The convention is that all-lower-cased names are used for pragmas such as strict, warnings, and others.


      I seem to still be stuck. Hear is my "module" now:

      package pack; use Exporter; @EXPORT = "$thing"; our $thing = "thing"; 1;

      And here is my program:

      use mine::package qw($thing); print "hello $thing";

      My output ends up being hello instead of hello thing. Thanks for the help.


        If you turn on strict in your calling script, you'll get more information on what's going on. Basically though, you still haven't addressed this issue:

        Which module name? Your package is named here:
        package pack;
        You're invoking it with:
        use mine::package;

        The others have already gone into what needs to be resolved to fix that. I'd recommend re-reading their replies and follow their suggested references and read those too. It's all there, it's just not necessarily spelled out for you directly.

        EDIT: On second look, you've introduced a couple more issues as well with your changes. You should definitely turn strict back on in your package, as well as adding it to your test program. Turn on warnings everywhere as well. They will point out very clear issues, but the perl errors and warnings won't necessarily make perfect sense unless you know what exactly they're talking about. For that you definitely need to read those references.


Re: Writing Modules
by armstd (Friar) on Aug 23, 2011 at 06:03 UTC

    ^^ What Dave said, plus the added problem that you lexically (in this case file) scoped $thing with my. You'll want to use 'our' to accomplish what you're trying to do there, which is accessing it from code in a different file. 'our' will allow you to do that, my will not. Both will pass strict. I normally recommend my'ing everything possible, and use methods to access it. Then you're really in control, but slower.

    my $thing = "thing";

    Changes to:

    our $thing = "thing";


Re: Writing Modules
by Anonymous Monk on Aug 23, 2011 at 06:15 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://921811]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-06-19 13:13 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.