Hello Monks, I have been coding in Perl for about two months now, and am just now beginning to delve into OO programming in perl. I get the error "Can't call method "pop" on an undefined value at DoStuff.pm line 15" This is my code for the calling file:
use strict; require DoStuff; my $pop; my $hl; my $dogs = DoStuff->new(); #print 'Population to set?'; #$pop = <STDIN>; $dogs->pop(5); print $dogs->pop;
The following is the code for the class..
package DoStuff; use strict; ########################## sub new{ my $self = {}; $self->{pop} = undef; #$self->{health} = undef; bless($self); return $self; } sub pop{ my $self = shift; my $self->pop = shift; return $self->pop; }
Health will be implemented just as soon as I can get the most basic variable defined in the object. From what I have read, the object itself should be automagically passed as the first variable when calling a class method. However, when the subroutine pop is executed, it seems no argument is passed. Any help on why this isn't working would be very much appreciated. If it matters, it's for a programming class, and so it does need to be implemented as an OO program.

In reply to Object Awareness by wcpyro

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.