in reply to Fill an array in a module ?

Variables in a module can be exported (see Exporter) or they can be accessed from outside the module using their fully qualified name:

package MyModule; our @SharedArray = (1, 2, 3, 4); # shared my @PrivateArray = (5, 6, 7, 8); # not shared 1;
use MyModule; print MyModule::SharedArray[1]; # prints 2

In Perl, each module has its own namespace. This allows developers of modules and programs to name variables and functions without worrying about the names in programs or other modules.

Replies are listed 'Best First'.
Re^2: Fill an array in a module ?
by AnomalousMonk (Archbishop) on Sep 14, 2014 at 01:20 UTC
    print MyModule::SharedArray[1]; # prints 2

    Should be  $MyModule::SharedArray[1] The variable name still needs a sigil for full qualification.

    In Perl, each module has its own namespace.

    Each module can be given its own namespace (as shown in the code example via package) or, indeed, any namespace. It's not automatic.

      Each module can be given its own namespace (as shown in the code example via package) or, indeed, any namespace. It's not automatic.

      :) Yup its not automatic, but a "module" has a specific meaning:) A module is a namespace (package Foo) associated with a file (Foo.pm). A module is a package you can use or require. So if it doesn't do these things we usually don't call it a module :)

        It would be nice if "module" had a single, specific meaning throughout the world of Perl, but it doesn't. In my experience, it pretty much always means a .pm file, but that file may or may not contain a package and, if it does, the package may or may not have the same name as the file. A module usually contains a package of the same name, but not always.

      Thanks for the reminder. I hadn't been using namespace qualifiers for these modules. I'd gotten away with that, somehow. But as the script set grows, it'll likely get cumbersome to have distinct names for arrays and variables.

      When I looked over the set I'd already scripted, it was obvious that some of those names were painfully long - and a fair number already included a kind of 'namespace' module abbreviation prefix in them.

      Yes, I was still scripting Saturday night...actually, until early Sunday morning. I used to try an alarm clock to remind me I should sleep at some point, but I just kept turning it off...

Re^2: Fill an array in a module ?
by DarrenSol (Acolyte) on Sep 17, 2014 at 17:06 UTC

    Thanks for the information. I hadn't been using 'our' or 'my' in modules, but I see from your example why I should. And the namespace qualifying as well. A bit of a task to modify the modules, but thankfully I'm not so deep into this one that it's a mountain of script to go through:)

    As the old saying goes...well, how I think it should go, in this case:
    "If it ain't broke, don't fix it. But just because it ain't broke don't mean it's working properly."

      But if it's not working properly, then it is broke