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

Hi guys,
I have created a class module (thanks to all of you who suggested how). Now I have a small problem. So module is another file with .pm extention. When I have to use it, I have to import it by use Record; command.
When I try to run my original program, it says Can't locate object method "new" via package "Record" (perhaps you forgot to load "Record"?) at create links table.pl line 37.
When I use use strict, the error code is : Global symbol "@stopwordlist" requires explicit package name at create links table.pl line 7.
Can anyone help me with how to use class modules, or objects. Do I have to explicitly state every variable's package name?
Thanks.

Replies are listed 'Best First'.
Re: using class module
by serf (Chaplain) on Jan 22, 2006 at 18:19 UTC
    Hi perlbeginner10,

    by itself, that message:

    Global symbol "@stopwordlist" requires explicit package name at create links table.pl line 7.

    says that you haven't put a my @stopwordlist; or something similar (our, local etc) in your code before you mentioned the array @stopwordlist for the first time.

    When you're using use strict; you need to declare your variables first before you use them - that's most of the fun of using strictness! :o)

    When you have a more involved problem like the first question in your post:

    Can't locate object method "new" via package "Record" (perhaps you forgot to load "Record"?) at create links table.pl line 37.

    it's often hard if not impossible for us to show you exactly where the error that is causing it is in your code, unless you can post us the code that is throwing the error, which will give us all the help we need to help you.

    To do this, if you can you should strip out everything that's not part of the bit needed to re-create the error and just post that. If you're lucky you might even find that the process of paring the code down to the bare minimum may *show* you where the error is coming from and you won't need to ask for help then.

    Of course lots of us are keen and eager to help you, but there's always more satisfaction for you in figuring it out yourself than having the answer given to you by someone who already knows that bit - and perhaps you'll get a better grasp of how it works too! :o)

    As this code is code calling a module which you have written, it could be pointing to an error in the module itself rather than that code which it trying to invoke it, so it would help to include (a stripped down version of if nescessary) the module as well.

Re: using class module
by friedo (Prior) on Jan 23, 2006 at 04:08 UTC
    Does your Record.pm file have a package Record; declaration? If not, the file will be loaded and compiled by the use command, but no Record namespace will be created.

    BTW, it's probably a good idea, from a stylistic standpoint, to prefix your classnames with something so they don't conflict with other stuff. "Record" is pretty vague. Perhaps something like YourProject::FooRecord, where 'foo' is the type of thing the record is about, would be more descriptive.

    Finally, make sure everything compiles correctly under use strict, or you're begging for pain.

Re: using class module
by glasswalk3r (Friar) on Jan 23, 2006 at 12:29 UTC

    In those cases (that you receive an error message and don't have a clue about what it means) you can always use the the Diagnostics pragma including a line like this  use diagnostics; in your program code.

    Of course, after fixing your problems, don't forget to remove this line since it will keep your program running slower.

    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill