Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
> 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


In reply to Re^2: Procedural vs OOP modules by LanX
in thread Procedural vs OOP modules by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found