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

Hi again.

(I was going to post on perlguru.com but some database issue seems to have gone wrong with that site. That aside..)

Greetings again.

2 years ago I wrote a module with utilities that invoke it. It all works well but it was not properly organized for CPAN. So the code is all there but I need to start fresh and transplant code from the older module as appropriate. However, I also want to use this as an opportunity to practice Moose and Function::Parameters and other trappings of "Modern Perl". And at the gate I have run into question on how to proceed. Here is the stub code that my h2xs invocation created; I have joined lines to keep it clear.

package DBD::Informix::DBspaces; use 5.022001; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01';

(Yes, this is Informix oriented, for those who recognize the buzz words.) I'm planning three packages in here, with the "DBD::Informix::DBspaces" package referencing the other two. So now I have two questions:

- Do I need to specify "use Moose" in every package I define in that .pm file? Or will the one "use Moose" in the first package suffice for the other packages as well. Or how about "use Moose" even before the first "package" directive?

- All those "our" declarations and the "require", generated by h2xs: Do those need to be repeated in each package or is is sufficient to put them all up front, outside all the packages? I suspect that outside all packages will be fine but if it is not fine I will be greeted by a barrage of errors masking any syntax errors and other small stuff.

So this misunderstood less-than-genius seeks the experience of those who have already played in these fields and humbly thanks thee. (What's plural for thee? :-)

Replies are listed 'Best First'.
Re: Moose in a module created with h2xs
by haukex (Archbishop) on Feb 22, 2019 at 19:48 UTC
    Do I need to specify "use Moose" in every package I define in that .pm file?

    I'd say so. use Moose; will, among other things, import functions like has into the current package, and although I'm sure there are ways to be tricky about it, IMO the best thing really is to put a use Moose; in every package using it.

    All those "our" declarations and the "require", generated by h2xs

    It depends, OO modules usually don't export stuff as well, so personally I would remove the require Exporter; and the our declarations, except for our $VERSION.

    Also, note that it's usually considered good practice to only have one package per file. Since Moose also turns on strict and warnings, the bare minimum you need in a .pm file using it is:

    package Foo::Bar; use Moose; # your code here 1;

    (The use 5.022; might be helpful because it turns on the 5.22 feature bundle, if you're sure you want to limit the minimum Perl version to that.)

      Thanks, haukex.

      Since I will definitely be exporting a select set of functions I will require (ahem) that Exporter business. I will take your advice about the re-use of Moose in each package; some documentation (buried in Moose::Manual I think) seems to indicate that. The same docs kept giving examples with multiple packages in one source file. On the other hand, the constructs generated by h2xs seem safe to pull out in front of all packages. I'll have to write a test module and see how that plays out. I'll let y'all know what happened.

      Again, thanks.

Re: Moose in a module created with h2xs
by tobyink (Canon) on Feb 23, 2019 at 10:22 UTC

    The plural of "thee" is "you". French and German and a lot of other languages have different words for you singular and you plural. French and German in particular have an interesting feature that you can use the plural form of you when talking to a single person to convey extra respect. In English, we did the same thing with thou/thee as the singular and you as the plural/respectful version. But because English folk are so inescapably polite and apologetic, thou/thee disappeared and is now considered archaic/obsolete.

    (As an aside, some languages — such as Indonesian — also have different words for the inclusive and exclusive forms of "we". For example, if I say "we are going to the cinema", do I mean "you, me, and some other people are going to the cinema" or do I mean "me and some other people are going to the cinema, but not you"?)

    Anyway, in answer to your main question, do use Moose in each class you define and use Moose::Role in each role you define. For packages which are neither classes nor roles (e.g. packages which are just a collection of utility functions, constants, etc), use neither.

    And all the exporter stuff, you should only use in packages that you wish to use as exporters.

    Unless you're doing something really weird, those are three different exclusive kinds of modules — exporters, classes, and roles. You should probably never write a module that acts as both a role and an exporter, for example. (You know those Venn diagrammes? Don't write modules which live in the overlapping bits.)

    Although you don't say so explicitly, you seem to be implying that you'll define multiple packages in the same module file. While there's certainly no prohibition in doing this, it can introduce a few gotchas when it comes to trying to use those packages. It's usually best to stick to one-package-per-file unless you have a very good reason not to.

      French and German in particular have an interesting feature that you can use the plural form of you when talking to a single person to convey extra respect.
      That's definitely true in French (using vous instead of tu to convey extra respect), but that's somewhat inaccurate in German, where the "respectful" form of du is, as far as I can tell, Sie, which is more like the German for "they" (and also for "she" *). But I'll leave it to German native-speaking monks to confirm and elaborate further if needed.

      Update: * But the verb is conjugated at the 3rd person plural, so it appears it's really "they," not "she."

        No, you're right. It's a long time since I studied German. "Sie" means "they", but can be used as the polite form for singular or plural you.