Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Think about Loose Coupling
 
PerlMonks  

Re: correct usage of class attributes

by gjb (Vicar)
on May 05, 2004 at 09:59 UTC ( [id://350770]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to correct usage of class attributes

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-

Replies are listed 'Best First'.
Re: Re: correct usage of class attributes
by Anonymous Monk on May 05, 2004 at 10:12 UTC
    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-

        last question, I promise!!!

        What about if I want something like: $self->{friends} and I want it to be a list or $self->{pets} and I want it to be a hash? like dogs->mike, charlie->cat mary, sal thanks so much!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://350770]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.