http://www.perlmonks.org?node_id=350765

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello great ones, I'm trying to fully understand correct object oriented implementation of perl, and I have a question of class attributes. If I have the following:
package First; my ($string1, $string2, @array1, @array2); ... sub Function10 { $self = shift; $self->{string1}="here";#is this the correct way to set a class att +ribute $string2="there";#or is this? ... and what are the differences? @{$self->{array1}}=(1, 2, 3);#and what about this? } ...
As you can tell, I'm still a little wet behind the ears on the newbie side, but I would appreciate a gentle nudge in the right direction. gratci'

Replies are listed 'Best First'.
Re: correct usage of class attributes
by gjb (Vicar) on May 05, 2004 at 13:59 UTC

    There's two things: class attributes and object attributes. The former have the same value for each object of that class, the latter have values specific to each object.

    Setting a class attribute $string in method Function10 would be done by simply assigning a value to it, e.g. $string1 = "Hi there";. This assignment is kind of global to all instances of the class.

    Setting the attribute of an object would be done by $self->{attr_name} = $attr_vallue;. This assignment is specific to the object you called the method on.

    Hope this helps, -gjb-

      It does help, and thanks gjb. I have one more question (at leat), and I hope you don't mind. How do I decleare attribute names, such as:
      $self->{attr_name} = $attr_value
      in your example? Are those done outside the method area with  my or like this:
      ... $self->{height}=6; $self->{weight}=210; ...
      If so, can you have hashes and arrays? thanks SO much!

        You don't have to declare them. Simply assigning to $self->{height} will do the trick. After all, the object is just a Perl hash that is blessed, nothing more.

        Of course, you may want to initialize the object attributes to default values so that you know their state. In that case the constructor is a good place to do so (the class method conventionally called new.

        Note that this is just the simplest approach to objects in Perl and definitely not the cleanest of safest, but well, it's good to start with IMHO.

        Hope this helps, -gjb-

Re: correct usage of class attributes
by sgifford (Prior) on May 05, 2004 at 14:50 UTC
    You've already got an answer to your question, I think.

    But a serious problem with your code above is that you don't declare $self to be a local variable. The results of that could be quite unpredictable. If this is really how your code is (and not just a typo in the sample code), you have one $self variable for the entire class, and every time you call an object method, that will get overwritten. This probably won't cause problems very often in real life, but when it does, they'll be of the awful and hard to find variety. :)

    Instead, you should use:

    my $self = shift;
Re: correct usage of class attributes
by Fletch (Bishop) on May 05, 2004 at 14:00 UTC

    Your first is wrong (it's setting the value for the key "string1" in your instance), the second is actually setting $string2, and the third is similarly wrong (again mucking with the instance).

    You might want to look at Class::MethodMaker which will write accessors for you.

Re: correct usage of class attributes
by mutated (Monk) on May 05, 2004 at 14:11 UTC
    This sounds an awful lot like a homework question. Have you even tried setting the values in perl using your listed method and then read back the results? There are also several good tutorials like here and here .


    daN.
      honestly, you are correct, except I have already completed my assignemtn and handed it in. I specifically don't like when people use perlmonks for things like that. I was able to get my code to work, but wasn't really sure why it was working and wanted to know more.

      I don't take offenses to your assumption (mostly because you were close to being right, completely because I agree with your idealology). Thank you, I will check out the tuts.

Re: correct usage of class attributes
by dave_the_m (Monsignor) on May 05, 2004 at 14:06 UTC
    The term 'attribute' is a bit ambiguous when it comes to Perl; Perl5 usually uses this term for something a bit different. I'm assuming you're referring to some variables of which there is only one instance per class, and you want some class methods to get and set them. In which case, something like this:
    package First; my ($attr1, @attr2, ...); sub get_attr1 { $attr1 } sub set_attr1 { my $class = shift; $attr = shift } sub get_attr2 { @attr2 } sub set_attr2 { my $class = shift; @attr2 = @_ }
    Of course there are many ways of doing this; the above is just one example.
Re: correct usage of class attributes
by TomDLux (Vicar) on May 05, 2004 at 15:55 UTC

    Have you tried reading the 4 perldoc OO tutorials? Begin with perldoc perlboot.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA