$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:>*

In reply to Re: Acessing an object's variable by Perl Mouse
in thread Acessing an object's variable by gmantz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.