Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day packetstormer,

Superficially, what you're doing there is probably fine. You wrote "Suppose this simple code:" — unfortunately, I don't know to what extent you've simplified your original code for posting here.

Here's a few pointers (they're guidelines and recommendations; not rules and regulations):

  • You should put your modules in files that are separate from your scripts.
  • There should only be one module per file.
  • The standard filename extension for a Perl module is ".pm".
  • Typically, a module is not executed: you only need read and write permissions (cf. scripts, which also have execute permission).
  • A module needs to return a TRUE value: usually implemented by making the last line just "1;".

So, Storm.pm would have this general structure:

package Storm; ... # use statements, etc. sub new { ... } ... # other methods 1;

Accessing instance variables (e.g. $self->{varname}) from your scripts is generally a very bad idea which you should avoid: it breaks encapsulation, causes all sorts of maintenance problems, and will generally come back to bite you in the bum when you least expect it. Add accessor and mutator methods to your classes and call them from your scripts, e.g.

my $varname = $self->get_varname(); $self->set_varname($new_name);

Passing a hashref to a constructor (e.g. Class::Name::->new($hashref)) is fairly common practice. I don't see any cause for concern in doing this.

The code you have in new() is somewhat deceptive and may trip you up down the track. Both "$self->{storm_auth_login}" and "$self->{append_text}" look like instance variables, but they're not! At this point in your code, $self is an unblessed hashref: it's not until a few lines later that it becomes an instance of $class (i.e. bless $self, $class;). You may want to consider rewriting new() (and adding an additional instance method) something like this:

sub new { my ($class, $args) = @_; my $self = bless $args => $class; $self->init; return $self; } sub init { my ($self) = @_; if ($self->{storm_auth_login}) { ... } ... # other initialisation code here return; }

Now new() is clean and could be inherited by a subclass which has its own init() method.

There's lots of documentation on this subject. Search perldoc: perl for any entries matching "mod" or "OO".

You may also be interested in Moose (and related modules). That's probably getting a little off-topic from what you're asking about here.

-- Ken


In reply to Re: High level OOP query by kcott
in thread High level OOP query by packetstormer

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 making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found