in reply to Acessing an object's variable

$name is not a variable associated with an object. It's a package level variable - if you'd make several objects of the same class, they will all share the same name.

To make object level variables, you need to use the reference (the object itself) in some way. Traditional is it to stuff the attribute (the object variable) inside the reference, but that's considered bad by some programmers (including myself).

A technique would be to use inside-out objects:

package LaLa; use strict; use warnings; use Scalar::Util 'refaddr'; my %name; sub newlala { # No point in using prototypes for methods. my $class = shift; return bless \do {my $var}, $class; # Always use two-arg bless } sub setname { my $self = shift; $name{refaddr $self} = shift; } sub getname { my $self = shift; return $name{refaddr $self} } sub DESTROY { my $self = shift; delete $name{refaddr $self} } 1;
This prevents directly accessing the object variables - and that's considered a good thing.

You could do that with tradional OO programming. No doubt someone else will show you the dark way.

Perl --((8:>*

Replies are listed 'Best First'.
Re^2: Acessing an object's variable
by gmantz (Initiate) on Dec 13, 2005 at 11:00 UTC
    This is very good! Thanks a lot for your reply. I just realized that each call to setname() reset the $name value causing it to keep only the last one. I didnt know about refaddr by the way. So the code you wrote ties values to a hash with the refaddr as the key. This is very nice but it is not what i would call object orientation really. I have been writing in java for the past few years so this looks to me more like hack than an OO solution. Is this the best way to do what i originally intended to? (i find it a bit hard to fully understand perlboot/toot) Again thanks very much
      Perl OO is a hack. As Larry has said, it was an exercise in minimalism - it proves that you can do OO with extremely little support from the language.

      That's why OO in perl6 will look quite different - it has solved the problem of needing hacks to have objects with data.

      I'm afraid that as long as you hava a Java mindset (nothing wrong with that), Perl OO will remain a major disappointment to you. And I can't disagree with you - I don't like Java, but I vastly prefer its OO system over Perls'.

      Perl --((8:>*

        I'm afraid that as long as you hava a Java mindset (nothing wrong with that), Perl OO will remain a major disappointment to you.

        I always just figured I'd just plod along and use Perl5's hash-based objects until Perl6 really arrives, then happily switch over and use whatever OO-support Perl6 provides.