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

I want to create nested packages as bellow. How can i do?

package A;

package B;
1;

package C;
1;

1; #end package A

How can i dot this?! Also there should be global variable which all the pavkages can access.

Replies are listed 'Best First'.
Re: How to create Nested packages?
by Corion (Patriarch) on Jan 11, 2008 at 10:06 UTC

    You can't nest packages that way, packages don't nest.

    But if you want to create a lexical variable seen by all three packages, just declare it:

    perl -Mstrict -e "package PA; my $foo = 'hello'; package PB; use stric +t; print $foo;"

    But creating "package global" lexicals is always a design flaw in my opinion, as you will sooner or later want to modify this variable from the outside. You will want to either provide a method to change your "global" or make it a real global variable.

      And how can i call the subroutines present in package PA from package PB without using &PA::A()?
      Is there any other option than &PA::A()?

        See Perl's documentation, perlmod, for knowledge on modules and packages. Basically, you will want to export subroutines from one package into the namespace of other packages. See Exporter for the canonical solution.