Topics

Objects, Modules and Packages

Package basics

  • A package is a collection of methods
  • Lives in its own namespace
  • Package methods can be exported or called directly
    • Foo::bar()
    • Foo->bar()
    • bar() (if Foo exports it)

Module basics

  • A module is a file containing one or more packages
  • Most people use "module" and "package" interchangably

Object basics

  • An object is a blessed reference to a hash
    • It doesn't have to be a hash reference, but it's most common.
  • Blessing assigns a single class to an object
  • An object can be re-blessed
  • You can muck with the internals, but that doesn't mean you should

1;

  • Modules must end with a true value
  • It doesn't have to be 1
  • Packages don't have the same restriction

@ISA

Perl's object inheritance methods use @ISA to determine what classes a module inherits from. Years ago, inheritance was declared by modifying @ISA directly; now, most programs use the base pragma to declare inheritance.

The following are mostly equivalent:

    package Foo;
    require Wango;
    @ISA = ( "Wango" );

    package Foo;
    use base "Wango";

We want your feedback

If we can improve perl101.org in any way, please let us know with this form.

Your name
Your email
Your comment