Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Packages and Modules

by Sary (Novice)
on Mar 06, 2011 at 12:54 UTC ( [id://891676]=perlquestion: print w/replies, xml ) Need Help??

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

Been mingling through some basic perl, now having some .net programming experience I can relate to using Packages and Modules however now, Im stuck. Cant seem to understand the how to USE them, since I have to do everything manualy in perl..

Also the next left me wondering :

"The package stays in effect until either another package statement is invoked, or until the end of the end of the current block or file."

What?...HUH? package invoked? Dont you just stick in on the top of your file like:

use HTTP::Response;

codecodecodecodecode

And there you have it! All the functions in the Response package are at your disposal.

So, it seems I was wrong and whenever another module is included in your file than the previous dies? Cant be.

I might have mashed alot in here, still learning. I'm doing my reading but i've come to some stopsigns past which to travel, I need your throughoutly explained knowledge.

10x, Alex

Replies are listed 'Best First'.
Re: Packages and Modules
by ELISHEVA (Prior) on Mar 06, 2011 at 13:18 UTC

    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();
Re: Packages and Modules
by Corion (Patriarch) on Mar 06, 2011 at 13:12 UTC

    You are confusing the package statement and the use statement. They are not the same. You could also have found this by experimenting.

Re: Packages and Modules
by luis.roca (Deacon) on Mar 06, 2011 at 19:25 UTC

    In addition to the previous comments, (pay close attention as both ELISHEVA and Corion helped part a few clouds for me on this same topic), you should definitely spend time reading : Variable Scoping in Perl: the basics and the article it was inspired by Coping with Scoping. If you want to get a little more knowledge on name spaces and scoping, and you should, read Lexical scoping like a fox and perlfaq7 under (What's the difference between dynamic and lexical (static) scoping? Between local() and my()?) Although these may not seem to be within the scope of your question they are closely related as ELISHEVA demonstrates above.

    I've seen it written a few times and will paraphrase here that * "Until you understand contexts in Perl you will be miserable." Please do not underestimate the importance of this subject. A lot of things will make much more sense once you get a handle on this area.

    * Name spaces is not the only area you'll hear about contexts, ie: list and scalar context.

    Good luck!

    UPDATE: Added the asterisk note on context in Perl.


    "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." — Don Quixote
Re: Packages and Modules
by GrandFather (Saint) on Mar 06, 2011 at 19:50 UTC
Re: Packages and Modules
by repellent (Priest) on Mar 07, 2011 at 05:46 UTC
      "The package stays in effect until either another package statement is invoked, or until the end of the end of the current block or file."

      What?...HUH? package invoked? Dont you just stick in on the top of your file like:

    Multiple package statements are allowed in the same module. Here's an example below:
    $ cat My/Own/Package.pm package My::Own::Package; sub foo { 'in My::Own::Package' } package My::Other::Package; sub foo { 'in My::Other::Package' } 1; $ ls My/Other/* ls: My/Other/*: No such file or directory $ cat print_foo.pl #!/usr/bin/perl use warnings; use strict; use lib qw(.); use My::Own::Package; print "calling My::Own::Package::foo() returns: " . My::Own::Package::foo() . "\n"; print "calling My::Other::Package::foo() returns: " . My::Other::Package::foo() . "\n"; $ print_foo.pl calling My::Own::Package::foo() returns: in My::Own::Package calling My::Other::Package::foo() returns: in My::Other::Package

    Notice that use My::Own::Package; only tells Perl to load the appropriate My/Own/Package.pm module file. The module itself can do anything it wants (like installing a function in package My::Other::Package).
Re: Packages and Modules
by BrimBorium (Friar) on Mar 06, 2011 at 19:01 UTC
      None of the above.. Perlguru i think or whatever other perl oriented site.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found