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

Hi Perl Monks, I have created a perl module as below, the below code contains just a few lines of the code.
package Hello; use Exporter; @ISA = ('Exporter'); # create an Exporter object. # Export all variables and functions via EXPORT statement # updated for exporting Setup CLI keys and Variables @EXPORT = qw($x $TestCase $TSPass $ConfigFile $TCPass);
The variables that are tried to be exported have to be accessed within the functions in perl module as well as outside. Ex: If i have a function testpm in the perl module Hello then the variables have to be accessed in that function as well as in the functions of the script which uses this perl module. When i run my script i get an error as below.
Global symbol "$TestCase" requires explicit package name at Hello.pm l +ine 58.
Please Please help me with this; i am badly stuck at this point. Thank You.

Replies are listed 'Best First'.
Re: Problem with Perl Modules.
by sauoq (Abbot) on Oct 26, 2005 at 07:36 UTC

    Declaring that you are going to export a variable isn't the same as declaring a variable. Under use strict; you get an error when a global variable is used without being first declared. You can declare the variables with our (i.e. something like our $TestCase;) or use vars (i.e. something like use vars qw( $TestCase );)

    -sauoq
    "My two cents aren't worth a dime.";
    
      Thanks a ton for your help.It works after i declare the variables in our. Right now i am facing another problem; my perl module has functions too, and i want to access these functions from the scripts which uses the perl module.
      package Hello; use Exporter; @ISA = ('Exporter'); # create an Exporter object. # Export all variables and functions via EXPORT statement # updated for exporting Setup CLI keys and Variables our($x,$test,$TestCase); @EXPORT = qw($x $test $TestCase ReadConfigFile);
      In my perl module i have written the subroutine for ReadConfigFile. I have another perl script which uses this perl module Hello and invokes ReadConfigFile from the script. When i ran my script it gives me
      Undefined subroutine &Hello::ReadConfigFile called at C:\Test\Hello.pl + line 30.
      I do not know if i am missing out on something. Please help. Thank You

        I think we'll need a little more info before we can help with this. Unless I'm missing something, from what you've told us, it should work. I would first double check the spelling of your "ReadConfigFile" where you define it. You could get this error if your sub is actually typo'd as "ReadConfgiFile" for instance.

        Aside from your error, I do have a couple quick observations...

        • Your comment "create an Exporter object" doesn't describe what's going on in that line. It actually makes your Hello module inherit from Exporter.
        • Your @ISA and @EXPORT variables should also be declared using our.
        • You might want to consider using @EXPORT_OK instead of @EXPORTin order to avoid cluttering your scripts' namespaces by default.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Problem with Perl Modules.
by arunvelusamy (Monk) on Oct 26, 2005 at 08:11 UTC
    Have a general practice of including 'use strict;' , 'use warnings;' and 'use diagnostics;' which helps your to figure of declaration errors while programing.

      Given that he asked about perl reporting this error: Global symbol "$TestCase" requires explicit package name at Hello.pm line 58. you should be able to surmise that he already is running under use strict; ...

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Problem with Perl Modules.
by jesuashok (Curate) on Oct 26, 2005 at 07:49 UTC
    Hi nisha,

    In your casse the problem is very familiar to all the people who are familiar in coding using the module 'strict' You are using strict or warnings some where in the place where you call this package.

    Declare the variable as follows :-

    our @ISA = ('Exporter'); # create an Exporter object.

    our @EXPORT = qw($x $TestCase $TSPass $ConfigFile $TCPass);

    this will solve your problem

    "Keep pouring your ideas"