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

hello monks,

today i decided to bug you with some writing style tips. as it turns out so far all the programs i have developed were for company use only and no code left (officially) the company premises. so the writing practice wasn't actually the issue as long as me and my colleges were able to use the code. but now we think the code could actually be useful for wider crowd, so the formating could become an issue.

Let me first give you an example of the writing style i used so far. my code would be divided into classes like:

+------Sub_class1 Main_class ---+------Sub_class2 +------Sub_class3
and i was forcing that any class that was ever made had an interface in the main class if a user wants to use it. as an outcome much less code was needed to produce since some objects were actually inherited from previous classes. also this practice proved to be very nice way to organize the code and structured logs that every class was specific for. But.... as an nasty nus-product a very complicated object inheritance network was produced .so you can basically say that all the sub_classes are not actually the general type of classes that a programmer would expect (they could not act as a self standing 'objects'). oh yea, and all the inheritance is done over the main_class, so that any sub_class actually doesn't know form where it inherits the object method that it is using. example:
Main.pm use base qw(sub_1 sub_2 sub_3); sub_1.pm $self->seek_me(); sub_3.pm sub seek_me{ ## }
so basically what i have is a one main class that cannot completely function without all the sub_classes and any sub_class probably cannot function without the main+sub_... class(basically one class divided into a number of small script). my question now is :

How pissed would you be if you had to work with something like that ?????

the code is commented and more or less neat , but the problem emerges when you wish to extract a method into a separate class. if you didn't collect all the needed methods that your method requires you are scr....! and the methods are God knows where!!!

Thnx!!

UPDATE

Thnx ppl, well i must admit I was hoping for an answer :-"Oh ye sure, that's ok, don't sweat it...."

but these comments were more helpful and informative!!!

I'll look into Moose!!

Thnx!

Replies are listed 'Best First'.
Re: perl writing tips needed
by moritz (Cardinal) on Jan 18, 2010 at 15:26 UTC
    I'm not sure if I understood your problem correctly, but you might benefit from looking at roles.

    A role is a collection of methods (and maybe attributes). When you compose a role into a class, it looks (nearly) as if you had written the methods right in the class, the role is "flattened" into the class.

    So you could write roles that provide various methods, and then compose them into the "main" class (if you need it - it might also be possible to just compose it into the classes where you actually need them).

    see Moose::Role for a nice Perl implementation of roles.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: perl writing tips needed
by Your Mother (Archbishop) on Jan 18, 2010 at 22:05 UTC

    Depending on what the real code looks like I would probably be pretty pissed to inherit it. It seems like the code layout is fairly arbitrary. And while you will find some range of opinions on the topic, code comments are an extremely poor substitute for Pod and history files and I only welcome comments where they are brief and clarify business logic decisions or explain why a sub-optimal choice is necessary (dependencies, side-effect, legacy support) and not just bad code.

    moritz has probably given the best idea from what I can understand of the problem. Roles (via Moose) would allow (and really steer) better categorization/separation of parts of your code while making its end use more intuitive. Moose is not easy to dive into but it's really cool once you do and the community is supportive.

Re: perl writing tips needed
by repellent (Priest) on Jan 18, 2010 at 20:45 UTC
    In addition to roles, you may also want to take a look at Class::Virtual as a way of building virtual base classes. Between the two, roles and virtual classes should give you plenty to think of.
Re: perl writing tips needed
by dsheroh (Monsignor) on Jan 19, 2010 at 08:59 UTC
    If I'm following your question correctly, it sounds like a horrible mess of spaghetti which I would feel the need to refactor mercilessly at the first opportunity in hopes of imposing something resembling a sane structure onto it.

    So, to answer your question, I would be pretty pissed (in both the US and UK senses of the term) if I had to work with something like that.