Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: OO best practice basic questions

by Amblikai (Scribe)
on Aug 28, 2014 at 10:09 UTC ( [id://1098845]=note: print w/replies, xml ) Need Help??


in reply to Re: OO best practice basic questions
in thread OO best practice basic questions

Thanks, i've added the uneven list test into my constructor.

When using the obj_count, is there a way to make it a method of the object? I don't know if that makes sense but in order to call it i guess i have to use Numbers::get_count() for example. But then if i attach it to each object i'm guessing the object will only have the value at that time, so i'd have to be sure to only check the value in the last object created. Is that right?

Another question i have is with the value "result"

It is an internally created value. So do i initialise the value in the constructor? Or the first time it is used? For example if i call the method add_nums, the result is stored in result. Should that variable already exist?

What we be an example of a destructor?

Thanks!

Replies are listed 'Best First'.
Re^3: OO best practice basic questions
by choroba (Cardinal) on Aug 28, 2014 at 11:37 UTC
    is there a way to make it a method of the object?
    Why not, just try
    $obj->ob_count;
    i guess i have to use Numbers::get_count()

    No, the class methods are usually called like

    my $count = 'Numbers'->get_count;

    It just returns a value of a lexical variable in the package, so it's not attached to an object, but to the class.

    Regarding the result: it depends. The direct initialization is usually used when it's cheap or when you know you'll use the value sooner or later. For the other cases, the "lazy" approach is more appropriate, i.e. create the result only when asked to do so.

    What we be an example of a destructor?
    In this case, you should probably do
    sub DESTROY { $obj_count-- }

    Note that you don't have to call $obj->DESTROY, you just undef $obj.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

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

    No recent polls found