Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Procedural vs OOP modules

by choroba (Cardinal)
on Oct 28, 2021 at 23:08 UTC ( [id://11138157]=note: print w/replies, xml ) Need Help??


in reply to Procedural vs OOP modules

You probably already know you need to implement several subroutines. Is there any state that needs to be shared among them? If so, use OOP; if not, there's no need for it.

It's not that hard to start one way and switch to the other paradigm later if needed, e.g. when you discover you need subclassing or some kind of a testing trick (mocking, dependency injection) for a procedural module or you realise you haven't used $self in any of the methods.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Procedural vs OOP modules
by LanX (Saint) on Oct 28, 2021 at 23:28 UTC
    > Is there any state that needs to be shared among them? If so, use OOP

    Actually you can share state in the package variables, but only one set of them.

    This pattern is similar to a singleton object in OOP.

    (The modular interface of Data::Dumper is a good example for that, with the global config vars, like $Data::Dumper::Indent )

    But you are right, a very good reason to use methods instead of functions is if they all start to look like this

    package Do_Something my %repeated = init(); foo( X, %repeated); bar( Y, %repeated); ...

    then it's obviously better to write

    my $obj = Do_Something->new(%init); $obj->foo(X); $obj->bar(Y);

    especially if there is a chance that you might need to have different initializations in the same code

    my $obj2 = Do_Something->new(%other_init);

    ( see Data::Dumper again with it's alternative OOP interface, where the configs are encapsulated in the objects like $OBJ->Indent([NEWVAL]) or $OBJ2->Indent([NEWVAL]) )

    On a side note: I prefer to think about objects like actors which do things, not containers which have attributes.

    So reducing them to shared data is not the whole picture.

    Last but not least: PBP lists some criteria when to use what...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      For completeness: You can also share state between functions with the help of closures.
      - Ron
        Actually I'd rather prefer Dumper instances implemented as closures.

        The setup methods here are just constructors in disguise, no need to call them more than once.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^2: Procedural vs OOP modules
by Bod (Parson) on Oct 28, 2021 at 23:19 UTC

    So if I only want methods that take a value and return another value, it will be procedural...

    That's a beautifully elegant exppanation choroba - thnk you

      I don't think this is necessarily true. The whole "data encapsulation" thing is a little fuzzy in practice with Perl. I think most people can agree to this. Having that deref syntax is nice, particularly if you tend to use a lot of hash refs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138157]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-28 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found