biohisham has asked for the wisdom of the Perl Monks concerning the following question:

Ok Monks, this is just preliminary experimentation with learning Packages and modules, I'm printing the symbol table keys for a very basic package that's created and called within the same file. However, I'm trying to understand why the printing of the symbol table is repeated twice? is it because of the transition between the package namespace and 'main' namespace. I am assuming this since calling a package exports its symbol table to 'main', hence this code reads the package symbol table in the declared package and then reads the same symbol table after it's been exported with 'require' to 'main', can anyone correct and clear this conjecture please?!
print keys(%newPackage::),"\n"; print "\n\n"; require 'newPackage.pl'; #exporting the package namespace. newPackage::subroutine1; package newPackage; #creating the package and its namespace sub subroutine1 {print "*This is a new package called from within the +file*\n";}


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re: Packages symbol tables and namespaces:
by moritz (Cardinal) on Nov 03, 2009 at 15:14 UTC
    require 'newPackage.pl';  #exporting the package namespace

    What require does is not exporting something, but opening your script, and running it again - which is why you get output twice.

    I'd write that as

    use strict; use warnings; { #creating a package package newPackage; sub subroutine1 { print "*This is a new package called from within the file*\n"; } } print keys(%newPackage::),"\n"; print "\n\n";

    Also you should load packages with use, and exporting is done with Exporter.

    Perl 6 - links to (nearly) everything that is Perl 6.