Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Argh. I'm slowly stepping into the OO aspect of Perl, and I've hit a bump in the road. I've combed through the tutorials, and couldn't find anything wrong with the code below (unless my eyes are really blurry now from checking all my '{}'s and '()'s.
test.pl
use strict; use Fruit; my $apple = new Fruit::Apple; print "\nAPPLE = $apple->{Value}"; $apple->increase; print "\nAPPLE = $apple->{Value}";

Fruit.pm
use strict; package Fruit::Apple; sub new { my $class = @_; my $self = { Value => 0 }; bless $self, $class; return $self; } sub increase { my $self = shift; $self->{Value}++; } return 1;
The error I'm getting is:
Can't locate object method "increase" via package "1" (perhaps you forgot to load "1"?) at test.pl line 7.
Thanks in advance

Replies are listed 'Best First'.
You're going to kick yourself...
by RMGir (Prior) on Apr 10, 2002 at 16:33 UTC
    You're going to kick yourself :)
    my $class = @_;
    That sets $class to 1, the LENGTH of @_.

    You meant to type

    my ($class) = @_;
    or
    my $class = shift;

    --
    Mike
      Thank you!Thank you!Thank you!Thank you!Thank you!Thank you! :) =~Desertcoder
Re: A Bad Day to OOP :(
by demerphq (Chancellor) on Apr 10, 2002 at 17:18 UTC
    Hi.

    You need to turn on warnings as well as strict.

    Then you would see that return 1; probably isn't what you want to do. You want 1; all by itself.

    Also, maybe you should use Fruit::Apple; and not just use Fruit;

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      You want 1; all by itself.

      return 1; is clunky, but it works. return 0; doesn't. return 'false'; does.

      Also, maybe you should use Fruit::Apple; and not just use Fruit;

      There's nothing to import from Fruit::Apple, and the filename is Fruit.pm. The two commingled steps of require and import() make things a little confusing at times, but pure OO modules are free to do some really funky things.