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'

In reply to Re: What is the significance of a package by tobyink
in thread What is the significance of a package by vyeddula

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.