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

In Perl, can you achieve with a class what you can achieve with a module? If that is so, can one get by using just classes? What are the pros and cons?

Also, are Perl modules sort of like Java classes with ONLY static members/methods?

Replies are listed 'Best First'.
Re: Modules or Classes
by tobyink (Canon) on Jun 20, 2012 at 06:28 UTC

    In Perl (current versions anyway) a class is just a usage pattern for a package. That is, any package can be used as a class. (Though if the package is not designed to be used as a class, the results will probably not be especially pleasing.)

    Packages are Perl's version of namespaces, and are vaguely tied in with the idea of modules, though in fact you can define multiple packages in the same module; or span a package definition over several modules; or define a package in your main script, not in a module at all.

    There are plans to add a new object system to future versions of Perl (possibly starting with 5.18) which would not replace but complement the existing system.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Modules or Classes
by DStaal (Chaplain) on Jun 20, 2012 at 13:12 UTC

    A module is a loadable segment of code - no more, no less. A class is a specific type of code structure. Therefore a module can contain a class, or several classes, or no classes.

    It is typically useful to have one module per class, as that means you can load each class independently easily. But neither one requires the other. A module can be a collection of functions, or even just a segment of procedural code.

    Because in object-oriented Perl it is useful (and near-ubiquitous) to have one module per class, the two terms occasionally get used interchangeably in that context.

Re: Modules or Classes
by Anonymous Monk on Jun 20, 2012 at 06:16 UTC
Re: Modules or Classes
by locked_user sundialsvc4 (Abbot) on Jun 20, 2012 at 13:57 UTC

    Perl’s ever so pragmatic implementation is not textbook-complete.   Nor is it intended to be.   It is intended to haul freight.

      I don't want to derail this thread, but I want to point out (with kindness and the hope that you'll take this as constructive criticism) that this is exactly the kind of post that earns you downvotes and makes it hard for you to fit in here. First of all, it's not related to the question in any way that I can tell, so that alone could annoy people into voting it down. Second, it really says very little (is any language "textbook-complete," whatever that means?). Yet it manages to sound as if you know what all the developers of Perl intend for it. Try this sentence:

      "I like Perl because its focus seems to be on getting things done, more so than on having the most elegant implementation."

      See how that said basically the same thing you said, but presented it as an opinion, rather than a pompous and global claim? See how it softened the part that might sound like criticism by leading with the praise? Try couching things more like that, and I think your ideas will be better received. Better yet, stop and think before posting, "Am I just pontificating here, or am I actually trying to help the questioner with the problem at hand?"

      Aaron B.
      Available for small or large Perl jobs; see my home node.