Replies: 3 comments 7 replies
-
Or:
|
Beta Was this translation helpful? Give feedback.
-
In the feature-class branch, toplevel (called "unit") classes work absolutely fine: class Some::Name::Here;
use whatever;
methods go here... No need to indent the entire file one level for that pointless toplevel container block. |
Beta Was this translation helpful? Give feedback.
-
I'm a bit late to this party, but I believe it is still a valid question, and I don't really see an answer to the question of best practice for style. Did you come to any conclusion about that, @bscan? I agree with your comment that the block syntax lends itself to multiple small classes per file, but Perl Critic complains when you do that. I don't want to be a slave to Perl Critic, but I do try to follow it unless I have a good reason not to. But Corinna is still very new and a best style practice will surely emerge sooner or later. Chris Prather has a very good tutorial for a "roguelike" game, and that appears to use individual files for most classes, but he doesn't shy away from including multiple small subclasses within the same file. |
Beta Was this translation helpful? Give feedback.
-
Hi all, I'm excited for Corinna and appreciate the work everyone has done. I'm wondering about the global nature of classes, and the recommended style for file layouts.
In the spec, a class will generate a package of the same name without qualification. This means if a library uses an internal
class User
, then any code using that library will also have theUser
package.Currently, Perl solves this issue primarily by convention: each package has its own file and the package name matches the file name.
These conventions are enforced via Perl::Critic (defaults
Perl::Critic::Policy::Modules::ProhibitMultiplePackages
andPerl::Critic::Policy::Modules::RequireFilenameMatchesPackage
) and packages are coordinated globally via CPAN to ensure no two packages in any library have the same name.Corinna appears to deviate from this norm in two ways: first, reducing boilerplate (e.g. sub new) encourages smaller and more frequent classes. Secondly, encouraging a block syntax
class {}
also appears to encourage people to have multiple classes per file. I would expect Corinna to generate far more classes than files, which could easily result in name collisions across modules and within applications if people are not careful.Why not automatically qualify classes?
I'm sure this has been discussed, but I can't find the relevant discussion. The approach of prefixing the resulting package of a class with the package name of the file is an approach seen in Zydeco, and it uses factories to build the object instead of
PackageName->new
.Prefixing it with something else entirely is a trick used in LeoNerd's Struct::Dumb, which generates a constructor named
User()
instead of a package namedUser
(and places the actual package name elsewhere).Style recommendation
Alternatively, is there a style recommendation for how to use Corinna classes to avoid name collisions?
Specifically, if I'm working on a package
MegaCorp::Logistics
and I need aclass Ship
, should I create a new file MegaCorp/Logistics/Ship.pm and name the class similar to a package?With the code like:
Or will it be possible to skip the redundant package name entirely and remove the block syntax from
class
?Error Checking
Is it possible to add compile time errors for duplicate classes? Currently for normal packages and Object::Pad classes, duplicate declarations are assumed to be extensions of the same package. It would be nice to get an error like
Sorry, the class Ship is already defined in MegaCorp/CruiseLines.pm
as opposed to simply merging all the methods and fields.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions