kesea
asked on
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
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
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;
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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- >{variable 2}\n";
includefile.pm
#------------------------- ----
package includefile;
sub new()
{
my $self=bless {
variable1 => qq(1),
variable2 => qq(2),
};
return $self;
}
1;
#------------------------- ------
use includefile;
my $vars=new includefile;
print "$vars->{variable1},$vars-
includefile.pm
#-------------------------
package includefile;
sub new()
{
my $self=bless {
variable1 => qq(1),
variable2 => qq(2),
};
return $self;
}
1;
#-------------------------
ASKER
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\">wi ndow.locat ion=\"http://www.mysite.com/page.html\"</S CRIPT>";
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
$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\">wi
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
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.
Obviously you wouldn't want to print anything in front of the HTTP headers.
our $variable1=qq(1);
our $variable2=qq(2);
1;
require "includefile.pl";
print $includefile::variable1;