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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You have a common problem. You had a small mistake, you had a wrong idea about where the mistake was, came up with a theory, and from there you have careened to a number of erroneous conclusions. And now those conclusions leave you unable to understand or accept good advice when you get it.

ariels is right. You bless $Person::list into no class at all. This blesses it into the empty class, which is the root of the entire tree of package namespaces, and is better known as main. This keeps you from calling methods normally, so you get around this by directly specifying the package to start the method search in. And somehow you convinced yourself that the object didn't have private data like the books says it should, so you started using globals. (Probably you just wrote an accessor wrong somewhere, and had a wrong conclusion.)

Please accept that everything you think you know is probably wrong. It will make learning the truth much easier. Here is some of that truth.

When a function receives a method call, @_ holds the callee and then the arguments to the call. The callee is what the method was called on, which can be a class (ie package name) or an object (ie blessed reference). Normally only constructors get package names, and they are supposed to construct an object and bless them into that package. A method call looks like $object->bar(@stuff) (with the function getting an @_ that looks like ($object, @stuff)).

Now let's rewrite your code properly. First your module:

package Person; use strict; sub new { my $class = shift; my $self = {}; return bless($self, $class); } sub name { my $self = shift; $self->{NAME} = shift if @_; return $self->{AGE}; } sub age { my $self = shift; $self->{AGE} = shift if @_; return $self->{AGE}; } sub exclaim { my $self = shift; printf "I'm %s and i'm %d years old!\n",$self->{NAME},$self->{AGE} +; } 1; # Modules need to end in a true value
And the driver code.
#!/usr/bin/perl use strict; use Person; my $foo = Person->new(); $foo->name("Stefan"); $foo->age(20); my $bar = Person->new(); $bar->name("Barney"); $bar->age(3); $bar->exclaim(); $foo->exclaim();
Yes, the constructor that you thought didn't work, does. You typed it in wrong, blamed the wrong thing when strange stuff started to happen, and went further and further wrong from there. Points to take away.
  1. Be very hesitant to say that a well-regarded book is wrong. It is more likely that it is right and you merely misunderstood it. Try typing in exactly what it has.
  2. When you get advice back on elementary topics, try it before saying how it will or will not work.
  3. Before ever saying that things don't work like they are supposed to work, look for elementary mistakes. Like typos, or mixing code from 2 functions and getting a result that does something unexpected. (Like bless an object into the wrong package.)
  4. Use strict. It takes less work to use it than explain why you didn't. :-)

In reply to Re: Re: Re: Classes Are Killing Me by Anonymous Monk
in thread Classes Are Killing Me by straywalrus

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found