Perl with classes

Perl with classes

Originally developed as a procedural language, Perl was later adapted to support modern Object-Oriented Programming (OOP) with features like inheritance, virtual methods, and static methods.

Consider the following example:

It looks like typical object-oriented code, but it's actually Perl. Here, classes are based on packages, so to write a class, you need to create a package and add a constructor.

Perl doesn't have complex data types like structures, so to create an object of a class, you need to "bless" a reference to one of the basic types with the package name.

The name of the constructor can be anything, but it's conventional to use new. This object stores a list of students and allows manipulation of that list.

There are several types of methods in classes: static and virtual. ClassRoom::new() is a static method, which can only access the global state of the class itself and receives the package name as the first parameter.

Virtual functions, on the other hand, accept the object as the first parameter and perform operations at the instance level. Let's adjust the class with a new virtual method:

Receiving the object as the first parameter should be familiar to Rust or Python developers.

And, of course, there is a destructor. When an object has no more references, it will be destroyed, and you can specify a function to handle this:

Now, let's add some more complexity. Consider that the classroom might have a professor inside, and we need a method to check for their presence.

It's time to create a child class, ClassRoomWithProfessor, which can be used like this:

ClassRoomWithProfessor is a child of ClassRoom with some methods overridden. First, use the use base 'ClassRoom' keyword to specify the base class. The new constructor simply forwards everything to the parent class:

Here, the SUPER keyword tells the interpreter to call the method from the parent class.

Now, let's implement a new version of add_person:

Objects in Perl are built on top of existing abstractions, making them relatively easy to learn and use. However, debugging such code can be tricky. For example, there's no limitation on how a virtual method is called, so ClassRoom::add_person might be invoked in different ways:

Of course, the developer of such a class can cover these calls, but doing so can make the code much harder to maintain and understand.

Torsten Raudssus

In IT, there are no problems—only IT solutions. Or you can just throw AI at it.

6 个月

Oh wow! ?? You should check out the latest Perl constructs! Nowadays, you'd use Moose or Moo for that. Take a look: https://metacpan.org/pod/Moose. Plus, you can metaphorically ride Moose into battle! The meta system in Moose offers greater flexibility and extensibility compared to Python's more rigid meta layer. If you're curious and want to learn more, feel free to reach out—I'd be happy to help!

  • 该图片无替代文字

要查看或添加评论,请登录

Nikolai Kutiavin的更多文章

  • Golang generic implementation for Producer-Consumer

    Golang generic implementation for Producer-Consumer

    Producer-consumer design pattern is well-known in software development. In a few words, one object produces values, and…

    6 条评论
  • Factory Method in Python with blackjack and ?h?o?o?k?e?r?s? decorators.

    Factory Method in Python with blackjack and ?h?o?o?k?e?r?s? decorators.

    Design patterns help organize code in a better way, improving readability and maintainability. Many design patterns can…

    8 条评论
  • Non-blocking synchronization for std::vector

    Non-blocking synchronization for std::vector

    In my previous post, I described how to protect a std::vector using std::mutex. It was straightforward.

    13 条评论
  • Unit-test in C++: what should you know.

    Unit-test in C++: what should you know.

    Unit tests are important for a single reason - they prove that a single component works as expected in isolation. If a…

    9 条评论
  • C++ transactional memory

    C++ transactional memory

    Sometimes I feel like an archaeologist, and today I’ll share one of my findings: an old proposal for the C++ standard…

    24 条评论
  • Behavior vs Data concurrency protection

    Behavior vs Data concurrency protection

    Multithreading is one of the hottest topics in C++. However, the most crucial question isn't just about running code…

  • Why preprocessor directives are evil

    Why preprocessor directives are evil

    I'll start with a simple quiz: Do you think the code below is correct? Does it make more sense now? Preprocessor…

    42 条评论
  • r-value reference: when and why?

    r-value reference: when and why?

    C++ has two commonly used types of references: l-value and r-value. An l-value reference points to an object with a…

    6 条评论
  • Network transport protocol: reliability and message-orientation out of the box

    Network transport protocol: reliability and message-orientation out of the box

    If I asked you to name a reliable network protocol for the transport layer, what would be the first one that comes to…

    4 条评论
  • File-backed allocator for STL containers

    File-backed allocator for STL containers

    The STL is renowned for its modular design, allowing universal algorithms to be applied to a wide range of containers…

社区洞察

其他会员也浏览了