Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

creating class base modules

by Avox (Sexton)
on Dec 17, 2002 at 15:45 UTC ( [id://220550]=perlquestion: print w/replies, xml ) Need Help??

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

I'm relatively new to perl. Wrote some scripts but would like to make them into a class/module. I'm having trouble, however. I've come up with a very simple example that I'm having trouble getting to work. So if a wise monk could tell me what I'm doing wrong, I'd be grateful. I'm using ActivePerl 5.6.1 on NT.
-----test.pl------- use Cat; $mycat = cat->new(); $mycat->feed("fish"); exit; -----Cat.pm------- package Cat; use strict; use warnings; our $VERSION = '0.01'; our @ISA = (); sub new { my $class = shift; my $self = { Stomach => "empty" }; bless $self, $class; return $self; } sub feed { my ( $self, $food ) = @_; $self->{ Stomach } = $food if defined $food; return $food; } 1; __END__
When I run test.pl, I get this error: Can't locate object method "new" via package "cat" (perhaps you forgot to load "cat"?) at test.pl line 3. I hope I'm just doing something stupid. Thanks all!

Replies are listed 'Best First'.
Re: creating class base modules
by Fletch (Bishop) on Dec 17, 2002 at 15:47 UTC

    Read The Fine Error. 'cat' ne 'Cat'. Case matters.

Re: creating class base modules
by stephen (Priest) on Dec 17, 2002 at 15:49 UTC

    Capital C for Cat. Classes are case sensitive in Perl.

    So your code should read:

    $mycat = Cat->new();

    stephen

Re: creating class base modules
by adrianh (Chancellor) on Dec 17, 2002 at 15:48 UTC
    cat->new should be Cat->new. Package names are case sensitive :-)
      And for that matter, just about everything else is in Perl, too.

      Makeshifts last the longest.

Re: creating class base modules
by Avox (Sexton) on Dec 17, 2002 at 15:57 UTC
    DOH! Thanks guys/gals. /me slaps head a couple times for good measure. Thats what I needed folks. heh...
Re: creating class base modules
by aging acolyte (Pilgrim) on Dec 17, 2002 at 16:17 UTC
    On a similarly simple question regarding using classes.

    What are the advantages/disadvantages in using

    export= qw( &new &feed )
    or export_ok

    Do things need to be exported?

    thanks

    A.A. Update Sorry I realise that this is a bit off topic and should have appeared as an seperate question

      export forces the listed symbols into the callers namespace. export_ok allows the listed symbols to be pulled into the callers namespace. export_ok is nicer as it gives the user of your module the choice. See Exporter for details.

      Exports pollute the namespace of the module user. If you must export try to use @EXPORT_OK in preference to @EXPORT and avoid short or common symbol names to reduce the risk of name clashes.
      --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://220550]
Approved by adrianh
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found