Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: Re: correct usage of class attributes

by gjb (Vicar)
on May 05, 2004 at 14:20 UTC ( #350786=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: correct usage of class attributes
in thread correct usage of class attributes

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-


Comment on Re: Re: Re: correct usage of class attributes
Select or Download Code
Re: Re: Re: Re: correct usage of class attributes
by Anonymous Monk on May 05, 2004 at 14:26 UTC
    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!

      Simple: $self->{friends} = ['John', 'Marie'];, or to add something to that list: push(@{$self->{friends}}, 'Jane');.

      Same story for hashes: $self->{pets}->{dogs} = 'mike'

      You may want to have a look at the tutorials on references and data structures in the perldocs (since the site is down, I can't link you now).

      Hope this helps, -gjb-

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-02 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls