CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Is there a Perl equivalent for C's #include?

 



adampage
New User

Dec 3, 2002, 6:08 PM

Post #1 of 5 (630 views)
Is there a Perl equivalent for C's #include? Can't Post

Hi all,

I plan to design a web site where each page's content is generated by its own individual Perl script (e.g., home.pl, music.pl, games.pl). There are some global constants (e.g., web site name, email address, etc.) that must be available to each of these scripts. I'd like to continue to use the strict pragma, but I also want to avoid redundantly declaring each of these constant variables in each of my scripts. Is there a way I can include a "globals.pl" file in each of my other scripts, the way I might #include a .h header file in C?

It would also be nice if I could include some globally needed stub functions, such as "print_error()", in this "globals.pl" file.

Please forgive me if a solution to this problem has already been posted in the forum. I hunted for a while but couldn't find anything. Thanks for your time!

Adam [ adamfpage@attbi.com ]


davorg
Thaumaturge / Moderator

Dec 4, 2002, 12:13 AM

Post #2 of 5 (621 views)
Re: [adampage] Is there a Perl equivalent for C's #include? [In reply to] Can't Post

Depending on your exact requirements you could do it with "require", "use" or "do".

The best answer is probably to create a new module and use "use MyModule" in your programs. A skeleton for a simple module would look like this:

Code
package MyModule; 

use warnings;
use strict;

require Exporter;

our $VERSION = 0.01;
our @ISA = qw(Exporter);

# List of variables and functions to export
our @EXPORT = qw(a_function another_function $A_SCALAR @AN_ARRAY);

our $A_SCALAR = 'some value';
our @AN_ARRAY = qw(a list of values);

sub a_function {
print "This is a function\n";
}

sub another_function {
print "This is another function\n";
}

1;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Dec 4, 2002, 2:12 AM

Post #3 of 5 (618 views)
Re: [davorg] Is there a Perl equivalent for C's #include? [In reply to] Can't Post

You should more often than not use @EXPORT_OK rather than @EXPORT which promotes namespace pollution.


davorg
Thaumaturge / Moderator

Dec 4, 2002, 2:26 AM

Post #4 of 5 (615 views)
Re: [Paul] Is there a Perl equivalent for C's #include? [In reply to] Can't Post

I agreed entirely, but I was trying to keep things as simple as possible :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


adampage
New User

Dec 4, 2002, 12:47 PM

Post #5 of 5 (612 views)
Re: [davorg] Is there a Perl equivalent for C's #include? [In reply to] Can't Post

Thanks for your help, guys! You gave me exactly what I needed.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives