Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to create Nested packages?

by isha (Sexton)
on Jan 11, 2008 at 09:57 UTC ( [id://661843]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://661843]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found