In Perl, how do I put multiple class in a single .pm file

北慕城南 提交于 2019-12-22 17:39:31

问题


I have a question of Perl MooseX::Declare. I want to put multiple classes in a single .pm file. How do I make this file, and what will the filename be because that .pm file has multiple classes?

For example

use MooseX::Declare;
class Point {

   has 'x' => ( isa => 'Num', is => 'rw' );
   has 'y' => ( isa => 'Num', is => 'rw' );

   method clear {
      $self->x(0);
      $self->y(0);
   }
   method set_to( Num $x, Num $y) {
      $self->x($x);
      $self->y($y);
   }
}


class Point3D {

   has 'z' => ( isa => 'Num', is => 'rw' );

   method clear {
      $self->z(0);
   }
   method set_to( Num $x, Num $y, Num $z) {
      $self->x($x);
      $self->y($y);
      $self->z($z);
   }
}

How do I save this pm file -- by first class name or second class name?


回答1:


Since the one-file-per-class guideline is merely there to help you organize your own code, you can name the file anything you want.

That said, there's a good reason that guideline exists.

There's an interesting discussion on this topic over at perlmonks that you may want to review.

I would advise against multiple classes per file, unless you're putting them in a common namespace.

{
    package Geometrics;

    class Point {
        ...
    }

    class Point3D {
        ...
    }
}

Should you go that route, you could name your .pm file accordingly (i.e., Geometrics.pm).

Failing that, assuming your classes are related by theme (as they seem to be), perhaps something like Points.pm would be appropriate.

Edit:

As for how to "call or use class (Point, Point3D) from outside" or how to instantiate classes defined in your module:

# import the package
use Geometrics qw(Point Point3d);
# or
# use Geometrics ('Point' 'Point3d');


# instantiate a Point
my $point = Point->new();

# instantiate a Point3D
my $point3d = Point3D->new();

You could also import the package by doing either:

# import the package but do not import any symbols from it
use Geometrics ();

or

# While you can import the package this way, please do not do so.
# import the package with ALL symbols
use Geometrics;

but there are going to be some caveats to doing so...

# importing the package with no symbols
use Geometrics ();

# means instantiation of objects must be fully-qualified
my $point = Geometrics::Point->new();
my $point3d = Geometrics::Point3D->new();
# importing the package with ALL symbols
use Geometrics;

# makes instantiation of objects simpler
my $point = Point->new();
my $point3d = Point3D->new();

# BUT because all symbols are imported with the package,
# overwriting of symbols can occur too easily.
# If I were to import the package Foo this way (which has the symbol Bar defined)
use Foo;

# and then import the package Baz this way (which also has the symbol Bar defined)
use Baz;

# I no longer have access to Foo:Bar as that has been over-written by Baz::Bar
# because I imported the packages with ALL symbols by default.
# The lesson here is to code defensively and:
#     only import those symbols you intend to use, or
#     don't import any symbols and fully-qualify everything.



回答2:


As a general rule do not put multiple classes in one file.

The exception I am used to seeing most often is related classes, especially helper or sub classes of the top level class. In those cases they have always been grouped into a single package and the file name being the package name.



来源:https://stackoverflow.com/questions/46473219/in-perl-how-do-i-put-multiple-class-in-a-single-pm-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!