• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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
0
kesea
Asked:
kesea
  • 4
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now