Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl and Files

Status
Not open for further replies.

xaze

Technical User
Dec 2, 2002
8
US
Hello,

I have a Web application that I am currently working on and it will require a config file. This is how the program will be work.

User goes to
File 1.

use module;
require 'config.pl'
require 'file2.cgi'

#do something

File 2
use module;
require 'config.pl'

#do something

Config.
use constant CONSTANT => 'VALUE'


Question: How would I make it so that by using require (or use) that all the constants in Config would be made available to use without using a fully qualified name (so, say CONSTANT instead of main::CONSTANT)? Also, I would liket o know if the "use module" in file 1 and 2 requires moer memory than not including "use module" in file 2... (So, if 'module' exported variables would it require more memory to have it included twice?)
 
If you need me to elaborate or clarify let me know. I could really use some help here.
 
Right, sorry about delay. Was sort of hoping someone else would answer you so I didn't have to remember how to do it <wry smile>

First, a module like this:
[tt]
#
# Module Myconstants.pm
#

package MyConstants;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(CONST1 CONST2);

use constant CONST1 => 'Hello';
use constant CONST2 => 'World';

1;
[/tt]

And then a Perl script like this
[tt]
#
# modtest1.pl
#

use MyConstants;

print &quot;&quot;, CONST1, &quot; &quot;, CONST2, &quot;\n&quot;;
[/tt]

That seems to do what you need, shout up if I misunderstood you.

Have a look at the Exporter documentation, it seems quite usable. You have to take a bit of care to make you don't get your variable names in a twist, just be careful what you use in the module and you won't get confused.
Mike

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top