Question about Modules and Variable Scope

D

Dean Hannotte

I'm writing a content management system and I've put some generally useful
routines into a common library that I invoke by starting my scripts with
'use cms;'. I then use a configuration file to initialize certain global
variables by coding 'require config.pl'. The problem is, I can't figure out
how I can get the library routines to see the globals that I've set in
'config.pl'. When I refer to these values from any of the library routines,
their values are undefined. I guess segregating code into a Perl module puts
it into a completely separate namespace.

If I code 'require config.pl' from inside 'cms.pm', then the library sees
the values, but the script doesn't! So I guess there's an internal mechanism
to prevent the loading of the same 'require' more than once in the same run?

How can I get the library, 'cms.pm', to see the same values that my scripts
see when they 'require config.pl' without having to pass these values as
subroutine parameters? Thanks!

Dean Hannotte
http://www.hannotte.net
 
A

Anno Siegel

Dean Hannotte said:
I'm writing a content management system and I've put some generally useful
routines into a common library that I invoke by starting my scripts with
'use cms;'. I then use a configuration file to initialize certain global
variables by coding 'require config.pl'. The problem is, I can't figure out
how I can get the library routines to see the globals that I've set in
'config.pl'. When I refer to these values from any of the library routines,
their values are undefined. I guess segregating code into a Perl module puts
it into a completely separate namespace.

If I code 'require config.pl' from inside 'cms.pm', then the library sees
the values, but the script doesn't! So I guess there's an internal mechanism
to prevent the loading of the same 'require' more than once in the same run?

How can I get the library, 'cms.pm', to see the same values that my scripts
see when they 'require config.pl' without having to pass these values as
subroutine parameters? Thanks!

Make config.pl export the variables and import them as needed.

perldoc Exporter.

Anno
 
A

Anno Siegel

Dean Hannotte said:
I'm writing a content management system and I've put some generally useful
routines into a common library that I invoke by starting my scripts with
'use cms;'. I then use a configuration file to initialize certain global
variables by coding 'require config.pl'. The problem is, I can't figure out
how I can get the library routines to see the globals that I've set in
'config.pl'. When I refer to these values from any of the library routines,
their values are undefined. I guess segregating code into a Perl module puts
it into a completely separate namespace.

If I code 'require config.pl' from inside 'cms.pm', then the library sees
the values, but the script doesn't! So I guess there's an internal mechanism
to prevent the loading of the same 'require' more than once in the same run?

How can I get the library, 'cms.pm', to see the same values that my scripts
see when they 'require config.pl' without having to pass these values as
subroutine parameters? Thanks!

Make config.pl export the variables and import them as needed. The
variables have to be package variables for that.

perldoc Exporter.

Anno
 
D

Dean Hannotte

Are you implying that I should turn 'config.pl' itself into a module (i.e.
'config.pm') and then issue 'use config;' both in my scripts and from inside
'cms.pm'?

In that case, might it not be easier to merge 'config.pm' and 'cms.pm' into
one file? Thanks!

Dean
 
J

Joe Smith

Dean said:
'use cms;'. I then use a configuration file to initialize certain global
variables by coding 'require config.pl'.

Are you aware that 'use' and 'require' occur at
different times during the compilation process?

require 'config.pl'; # This is done *after* the 'use'
use cms; # This is done *before* previous line
BEGIN { print "This runs after 'use' but before 'require'\n"; }

-Joe
 
A

A. Sinan Unur

[ please do not top-post ]
Are you implying that I should turn 'config.pl' itself into a module
(i.e. 'config.pm') and then issue 'use config;' both in my scripts and
from inside 'cms.pm'?

I doubt Anno is recommending that you use the name 'config.pm'.

Sinan
 
A

Anno Siegel

[top-posting rearranged. please don't do that]


[...]
Are you implying that I should turn 'config.pl' itself into a module (i.e.
'config.pm') and then issue 'use config;' both in my scripts and from inside
'cms.pm'?
Yes.

In that case, might it not be easier to merge 'config.pm' and 'cms.pm' into
one file? Thanks!

The purpose of a configuration file (whether technically a module or
not) is to keep some declarations and definitions apart from the
rest of he software. Incorporating it in the main module means
you won't have a configuration file but a configuration section
in the module.

Your call.

Anno
 
A

Anno Siegel

[top-posting rearranged. please don't do that]


[...]
Are you implying that I should turn 'config.pl' itself into a module (i.e.
'config.pm') and then issue 'use config;' both in my scripts and from inside
'cms.pm'?

Yes, though module names should begin upper-cased. Lower-case names
are reserved for pragmas.
In that case, might it not be easier to merge 'config.pm' and 'cms.pm' into
one file? Thanks!

The purpose of a configuration file (whether technically a module or
not) is to keep some declarations and definitions apart from the
rest of he software. Incorporating it in the main module means
you won't have a configuration file but a configuration section
in the module.

Your call.

Anno
 
A

Anno Siegel

A. Sinan Unur said:
[ please do not top-post ]
Are you implying that I should turn 'config.pl' itself into a module
(i.e. 'config.pm') and then issue 'use config;' both in my scripts and
from inside 'cms.pm'?

I doubt Anno is recommending that you use the name 'config.pm'.

I almost did, overlooking the non-conformal spelling. Thanks for
the pointer.

Anno
 
S

Sherm Pendley

Anno said:

config.pm is a bad choice of module names though. On OSs that normally
use case-insensitive file systems (Windows, Mac OS X), it will clash
with the standard Config.pm module.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top