in reply to Packages with subs using fully qualified class names, not always their own.

SimonSaysCake:

It's one of the advantages of perl, IMO. One of the problems with object-oriented programming, as I see it, is that there's an internal pressure to make your classes "complete" so that users can do all the tasks they would reasonably expect to be able to do with the class. Many languages require that a class be created in a single source file, so frequently you'll get occasionally useful functionality thrown into the file, when most applications don't actually need it. As the class ages, more and more stuff gets put in there.

The ability to add some functionality to a class without inheriting from it is pretty nice, as you can simply include other modules at the top of your program. So if you need some rarely-needed functionality, you don't need to subclass something to add the functionality and then update your script to use the new subclass. Using your extra functions with another package of extra functions is similarly easy, just add the functions into your class and you're done. No need to refactor your application, no need to worry about how to refactor the extra functions into yet another class hierarchy to let them coexist.

If it's truly a one-off function, you can just add it into your script, and it'll still act as if it were part of the package proper.

It's not something you'll need to do all the time, but it's flexible and clean, compared to having to worry about inheritance headaches.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re: Packages with subs using fully qualified class names, not always their own.

Replies are listed 'Best First'.
Re^2: Packages with subs using fully qualified class names, not always their own.
by karlgoethebier (Abbot) on Jul 01, 2017 at 08:34 UTC
    "...The ability to add some functionality to a class without inheriting from it is pretty nice..."

    Yes. But isn't this the thing about "what a class does"?

    Perhaps it might be better in such a case to use a plugin: Let it role, baby, role.

    Just some callow thoughts. Please correct me if i'm wrong.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      karlgoethebier:

      You're not wrong at all. Roles could be a good way to do it.

      Generally I do it when:

      • It's purely a one-off thing that I'm trying to solve, and don't expect to ever need it for the class again. A frequent case is when I'm debugging something complicated that relies on a little bit of inner state of the class and a custom "toString" function in the class for logging could give me the data I need to solve the problem.
      • The class API isn't fully resolved and some of the implementation is a bit hacky. I don't want to expose the hackiness to the classes API but I need to get a problem solved. So I have no problem accessing the internals in an ugly fashion to make something happen, and let the class evolve normally without putting an ugly hack in the API that may be troublesome to remove later. (Such as when John Smith sees it and immediately uses it in production....)

      I'm not saying it's a good/best practice, but that it sure is convenient.

      Sorry for the long delay in replying. I've ignored my Chatterbox queue a bit too long, and now I'm trying to work through the backlog...

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        "...I'm not saying it's a good/best practice..."

        Thank you very much roboticus for your kind reply and the good advice.

        I'm convinced that simplicity is one of the hardest things to accomplish - in any metier.

        And i think that the combination of Class::Tiny/Role::Tiny is very close to this magic "One Size Fits All" pattern AKA the Holy Grail ;-)

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help