Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Perl Object Initilization

by tospo (Hermit)
on Jun 10, 2011 at 10:02 UTC ( #909082=note: print w/ replies, xml ) Need Help??


in reply to Re: Perl Object Initilization
in thread Perl Object Initilization

you should not redefine $self once you have blessed it into objecthood. Assign to individual keys of the underlying hash instead. In addition to the method given by chromatic above, you can also do it this way (just a slightly different style):

sub init { my $self = shift; my %args = @_; while (my ($attribute, $value) = each %args){ $self->{$attribute} = $value; } }
Here yuo are using the fact that an even-sized list can simply be assigned to a new hash to populate it ( which is what you always do when you say my %hash=( key1 => value2 ) as '=>' is just a glorified comma).
I would also agree that, particularly in this case, a separate init method is not needed, because you are just assigning values to attributes and that can all be done when you construct the hash that you then bless in the 'new' method.
There may be some cases where more complicated setup needs to be done that might be better off in separate subs, such as connections to various databases, but then I would call them things like '_init_db_connection' or whatever you need to do.
I would also second chromatic's suggestion to look into Moose soon. If you are just starting with OO programming in Perl then it's certainly good to see the non-Moose, old style first and understand it but then move on to a more modern object system like Moose. It seems like a bit of a learning curve at the beginning but it's well worth it.


Comment on Re^2: Perl Object Initilization
Download Code
Re^3: Perl Object Initilization
by chromatic (Archbishop) on Jun 11, 2011 at 16:06 UTC
    If you are just starting with OO programming in Perl then it's certainly good to see the non-Moose, old style first and understand it but then move on to a more modern object system like Moose.

    I think that's backwards, especially if you've not done OO before. Perl 5's default OO system exposes so much plumbing it's easy to get lost in the syntactic details of how to do things instead of understanding well what to do and why to do it.

    That's kind of like suggesting that you make your own pie crust by hand before you can ever put toppings on a bake at home pizza.

      absolutely, no way you can put toppings on pizze before you can do the crust!!! :)
Re^3: Perl Object Initilization
by PyrexKidd (Monk) on Jun 11, 2011 at 23:32 UTC

    This is a simplified version.

    In this particular case, you are saying that it is better to pass a hash (or hash ref) and build any attributes/values (using a loop) because if you have an uneven list it will cause a mismatch of keys/values?

    on the note of 'initialize as needed', would you define the respective initialization subroutines in the class definition, then call them from the code where you require the object?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-11-26 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (160 votes), past polls