On Fri, 4 Aug 2017 15:23:21 +0200
hw <h...@gc-24.de> wrote:

> Now I´m confused as to what is a module and a package.  Both are
> files.
> 
> In any case, it is my intention to keep everything that one class
> requires within one file which starts with 'package FOO'.  Due to
> increasing complexity, I´m using POD sections or I wouldn´t know
> anymore what I´m doing ...
> 

A package is a name space. A file may have more than one package:

package Foo;
# thingies for Foo

package Bar;
# thingies for Bar

package main;
# thingies for main (default package)

__END__


In Perl, a class is a thingy blessed to a package. In other words,
classes are subset of packages but a package need not be a class.

package Foo;
$object = bless $thingy, Foo;

__END__


A module is a file which has the extension `*.pm` It can be loaded by
the `use` keyword.

use Foo; # load the module Foo.pm

__END__

As seen above, a module need not be restricted to one package. In fact,
the module need not have the package for its name.

# File: Foo.pm

package Bar;
# thingies for Bar

__END__

And a module can have one (or more) class(es).

# File: Foo.pm

package Foo;

sub new {
    return bless { @_ };
}

__END__


Packages may be distributed among many files.

# File: Foo.pm

package Foo;
# thingies for Foo

__END__

# File: Bar.pm

package Foo;
# more thingies for Foo

__END__



This is very confusing so the convention is that one module has one
package. I'm telling you this because the terms module and package are
not interchangeable.

To summarize:

* A module is a file with the extension *.pm

* A module may have zero, one or many packages but please stick to one
  package per module.

* Packages may be distributed among many files but please stick to one
  module for each package.

* A package may be a class. If it has the keyword `bless`in it, it is a
  class.



> > The way I would do it would be to separate out what is common in
> > both Foo and Bar and put them in a separate module. Then Foo and
> > Bar can `use` it as their parent.  
> 
> I think that might be the best solution.  I could make a metaclass
> that contains the methods common to both FOO and BAR and have both of
> them descend from it.
> 
> However, that feels like using inheritance in an upside down manner.
> But perhaps it´s supposed to be used like that?

Yes, they are called virtual classes. Aka generics in Java and
interfaces in general.
http://www.cs.utah.edu/~germain/PPS/Topics/interfaces.html


-- 
Don't stop where the ink does.

        Shawn H Corey

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to