Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question about Modules and Variable Scope

Reply
Thread Tools

Question about Modules and Variable Scope

 
 
Dean Hannotte
Guest
Posts: n/a
 
      05-23-2005
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


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      05-23-2005
Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      05-23-2005
Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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

 
Reply With Quote
 
Dean Hannotte
Guest
Posts: n/a
 
      05-24-2005
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

"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:d6sk51$nio$(E-Mail Removed)-Berlin.DE...
> Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > 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
>



 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-24-2005
Dean Hannotte wrote:

> '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


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-24-2005
"Dean Hannotte" <(E-Mail Removed)> wrote in
news:MMAke.709$(E-Mail Removed):

[ please do not top-post ]

> "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> news:d6sk51$nio$(E-Mail Removed)-Berlin.DE...


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

>
> 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. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-24-2005
Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:

> "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> news:d6sk51$nio$(E-Mail Removed)-Berlin.DE...
> > Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:


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


[...]

> > > 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.


> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-24-2005
Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:

> "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> news:d6sk51$nio$(E-Mail Removed)-Berlin.DE...
> > Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:


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


[...]

> > > 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.


> 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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-24-2005
A. Sinan Unur <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "Dean Hannotte" <(E-Mail Removed)> wrote in
> news:MMAke.709$(E-Mail Removed):
>
> [ please do not top-post ]
>
> > "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> > news:d6sk51$nio$(E-Mail Removed)-Berlin.DE...

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

> >
> > 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
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      05-24-2005
Anno Siegel wrote:

> Dean Hannotte <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>>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.


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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments