Post the code that lead you to believe that the Cookbook was wrong, and I will try to get back to you Monday evening with an explanation of why you got an error if nobody else does first.

Based on what I know, I find it very likely that your mistaken code was something like:

sub new { my $class = shift; return bless({},shift); }
which resulted in an error message like:
Can't locate object method "name" via package "main" at line 4.
Very confusing. So you you fiddled around with the code, somehow decided that you needed to use a global before eventually hitting on changing that line by adding Person:: to the method name, which hid the underlying problem. Your experiments probably were mostly variations on the theme of telling it that you want, "Person, not main you moron!" And by the time it finally listened, you had what you posted.

BTW that was not meant as a flame. It describes a common programming trap. Something doesn't work like it is supposed to, blame is assigned early, and the person goes into a pattern of somewhat random experimentation until something (hopefully) works. This is a natural tendancy but is horrible for programming. You need to change one thing and only one thing at a time. And random changes are probably bad ones.

In this case, when you tried to type in examples from the book and they didn't work, you should have looked closely for how your code didn't match, and if you couldn't see it then post showing your exact code, your error message, name the book and page you are trying to copy, and say, "I don't understand why this is giving me this error message, the book says it should work?" Then someone would have told you what your mistake was, and why your mistake resulted in that error.

Just remember this for the future. If you find yourself doing random things to get something to work, you are trapped and need to back up, break down what is happening into small logical pieces, take a break, or get outside help. Random is bad.


In reply to Re: Re: 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":



  • 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.