perl includes/require

Hello Experts,

If I want to have the same variables declared at the beginning of many different PERL files, as below, is there a way to have them in just one file and "include" this file in all the necessary files?  It would be much easier this way for me because that way when I want to change one or both of their values, I only have to do it in one place as opposed to each file.

my $variable1=qq(1);
my $variable2=qq(2);

If the solutions is with a require, as in :

require "includefile.pl";

could you give a working example.  I have seen examples where the includefile.pl contains functions, but not just variables, and when I tried to copy the examples, it didn't work.  

ie includefile.pl is as follows:


package includefile;

my $variable1=qq(1);
my $variable2=qq(2);

1;


but when I include this file with a require in another file, it says $variable1 and $variable2 are not defined (I am using strict).

Any suggestions as to what I can try to get this working?

Thanks,
kesea
keseaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
package includefile;

our $variable1=qq(1);
our $variable2=qq(2);

1;


require "includefile.pl";
print $includefile::variable1;
0
ozoCommented:
use includefile;
print "$variable1,$variable2\n";

includefile.pm is as follows:

package includefile;
require Exporter;
@ISA = qw(Exporter);
@EXPORT    = qw($variable1 $variable2);
our $variable1=qq(1);
our $variable2=qq(2);
1;
0
ozoCommented:
use includefile qw($variable1 $variable2);
print "$variable1,$variable2\n";

includefile.pm is as follows:

package includefile;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK    = qw($variable1 $variable2);
our $variable1=qq(1);
our $variable2=qq(2);
1;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

godspropyCommented:
You can create a %hash in the include file that is returned to the caller. However, the variables will become like $vars->{variable1}.

use includefile;
my $vars=new includefile;
print "$vars->{variable1},$vars->{variable2}\n";

includefile.pm
#-----------------------------
package includefile;

sub new()
{
my $self=bless {
        variable1 => qq(1),
        variable2 => qq(2),
        };
return $self;
}

1;
#-------------------------------
0
keseaAuthor Commented:
ozo, your last suggestion worked except that print "$variable1,$variable2\n"; seemed to be equivalent to a print "Content-Type: text/html\n\n"; statement in that the following code no longer worked to redirect the web page to somewhere else:

$strJumpURL =qq(http://www.mysite.com/page.html);
print "Location: $strJumpURL\n\n";
exit;

I thought at first it was no big deal, and replaced that code with the following:

print $q->header();
print  "<SCRIPT TYPE=\"text\/javascript\" LANGUAGE=\"Javascript\">window.location=\"http://www.mysite.com/page.html\"</SCRIPT>";
exit;

But now when this code gets called from script A (script A redirects to a cgi with this code), and this code just redirects to page.html, it redirects ok but in the process it flashes what's below to the user instead of flashing a blank page.  How can I get rid of it?  I would prefer a method where I can keep all of my print "Location: $strJumpURL\n\n" statements but I don't know if that is possible.  If it's not possible, maybe there is another way I can redirect things, besides the javascript and the print "Location..".  Or is there just a way to get rid of what flashes below.

Content-Type: text/html; charset=ISO-8859-1

I am thinking that  godspropy's solution will give me the same problem since it has a print "" in it too.

Thanks,
kesea
0
ozoCommented:
The print was just to demonstrate how the variables are defined in the program that calls the includefile.
Obviously you wouldn't want to print anything in front of the HTTP headers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.