http://www.perlmonks.org?node_id=980391

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

Please explain me the significance of packages in perl.A simple code will be appreciated

Replies are listed 'Best First'.
Re: What is the significance of a package
by davido (Cardinal) on Jul 07, 2012 at 00:09 UTC

    It seems silly for us to spend time and effort composing an explanation of packages, and sample code, when both are provided in Perl's documentation, here: perlobj, where it says, "A package is simply a namespace containing variables and subroutines."

    perlmod is also useful.

    Now if your question was, "I've read perlobj and perlmod, but am having trouble understanding xyz, then it wouldn't seem like duplicated effort.

    So go ahead and look them over. Let us know what questions they don't settle.


    Dave

Re: What is the significance of a package
by tobyink (Canon) on Jul 06, 2012 at 23:03 UTC

    Packages provide a namespacing mechanism. Package A can define a function called "output", and package B can define a function called "output", and both packages will see their own output function. Example:

    use 5.014; package A { sub stuff { output($_) for qw(1 2 3 4) } sub output { say "A: @_" } } package B { sub stuff { output($_) for qw(2 4 6 8) } sub output { say "B: @_" } } package main { A::stuff(); B::stuff(); }

    Packages are also used for object-oriented programming in Perl. A reference to a variable can become associated with a package (a process known as blessing), after which that reference is considered to be an object. Objects can have method calls performed on them, in which case the method is resolved to a function defined in the package associated with the object. (Or, if there is no such function, potentially a function in a parent class.)

    use 5.014; package C { sub foo { say "foo!" } } package main { my $object = bless []=> 'C'; $object->foo; }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: What is the significance of a package
by zentara (Archbishop) on Jul 07, 2012 at 10:56 UTC
Re: What is the significance of a package
by muba (Priest) on Jul 06, 2012 at 23:05 UTC

    One important feature is that packages give you namespaces. Entries with identical names can co-exist because they live in different namespaces. As to demonstrate:

    ### my-perl-script.pl use strict; use warnings; use Bar; use subs qw(foo); foo; sub foo { print "Foo, from the main package.\n"; }
    ### Bar.pm package Bar; use strict; use warnings; sub foo { print "Also foo, but this comes from the Bar package.\n"; print "(And no, if my wife calls, I'm not at the bar!)\n"; }

    Try to do this without packages, and Perl will rightfully complain, "Subroutine foo redefined at ...". Which is hardly ever what you want.

Re: What is the significance of a package
by monsoon (Pilgrim) on Jul 06, 2012 at 23:11 UTC
    In short packages are namespaces. A detailed explanation is worth a chapter in a book. Camel book for example. I recommend you get a copy and study it if you're serious about Perl.
Re: What is the significance of a package
by Anonymous Monk on Jul 07, 2012 at 23:52 UTC
    This sounds like homework.
      Possibly an interview question - and there's nothing wrong with wanting to be able to answer an interview question - but I do encourage reading up on things like this, and asking a more detailed question if there's confusion; being able to answer the questions, but not really use the concepts, may get you a job, but understanding them well will keep you this one and make it far easier to get the next one ("say, that programmer really gets this stuff").
      Homework on Perl?