Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Packages and Modules

by ELISHEVA (Prior)
on Mar 06, 2011 at 13:18 UTC ( [id://891680]=note: print w/replies, xml ) Need Help??


in reply to Packages and Modules

use doesn't assign your script of module file to a package. It simply makes the functions in another package available to your own code. If you think about it, use can't be defining your module's package because you can have lots of use statements at the top of your file (by the way those highlighted terms are links to the formal Perl documentation - click on them and you will see the actual definition of "use"). Which one would be the package?

The phrase "a package says in effect until..." is referring to this statement that you often find in the top of code files:

package Foo::Bar::MyFrobinicator;

What that statement does is declare a namespace. All of the our variables and subroutines that you define after the package Foo::Bar::MyFrobnicator statement belong to the "Foo::Bar::MyFrobnicator" package. So if you define a sub called "make_frobnicator", then its fullly qualified name will be "Foo::Bar::MyFrobnicator::make_frobnicator".

Of course, that is a lot of typing, so Perl gives you a shortcut. As long as the "Foo::Bar::MyFrobnicator" namespace is effect, you can just type "make_frobincator" and Perl will figure out that what you meant was "Foo::Bar::MyFrobnicator::make_frobnicator". (Perl is nice to people who don't like to type).

This namespace and all the benefits it gives stays in effect until your code includes another package statement, lets say package Foo::Baz, like this:

# if you don't want to waste a _lot_ of timing guessing # at mistakes that Perl could have told you about # always put these two lines at the top of your scripts # even before any other use or package statements use strict; use warnings; package Foo::Bar::MyFrobincator; #full name is Foo::Bar::MyFrobincator::make_frobnicator sub make_frobnicator { ... } # full name: $Foo::Bar::MyFrobnicator::current_frobnicator our $current_frobnicator; # Perl knows we mean to call # Foo::Bar::MyFrobincator::make_frobnicator() # and that we want to assign the value to # $Foo::Bar::MyFrobnicator::current_frobnicator $current_frobnicator = make_frobnicator(); # change the active namespace to Foo::Baz package Foo::Baz; # fullname: Foo::Baz::bye sub bye { print "bye-bye\n"; } # Since the active namespace is now Foo::Baz # Perl now thinks you mean Foo::Baz::make_frobnicator() # which doesn't exist so you get an error make_frobnicator(); # so we need to use those awful long names - ugh! $Foo::Bar::MyFrobnicator::current_frobnicator = Foo::Bar::MyFrobincator::make_frobnicator() # but we can use a short name here # because bye is part of the Foo::Baz package # so Perl knows we mean Foo::Baz::bye() bye();

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found