Question about Modules and Variable Scope

Discussion in 'Perl Misc' started by Dean Hannotte, May 23, 2005.

  1. 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
     
    Dean Hannotte, May 23, 2005
    #1
    1. Advertising

  2. Dean Hannotte

    Anno Siegel Guest

    Dean Hannotte <> 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
     
    Anno Siegel, May 23, 2005
    #2
    1. Advertising

  3. Dean Hannotte

    Anno Siegel Guest

    Dean Hannotte <> 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
     
    Anno Siegel, May 23, 2005
    #3
  4. 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" <-berlin.de> wrote in message
    news:d6sk51$nio$-Berlin.DE...
    > Dean Hannotte <> 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
    >
     
    Dean Hannotte, May 24, 2005
    #4
  5. Dean Hannotte

    Joe Smith Guest

    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
     
    Joe Smith, May 24, 2005
    #5
  6. "Dean Hannotte" <> wrote in
    news:MMAke.709$:

    [ please do not top-post ]

    > "Anno Siegel" <-berlin.de> wrote in message
    > news:d6sk51$nio$-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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 24, 2005
    #6
  7. Dean Hannotte

    Anno Siegel Guest

    Dean Hannotte <> wrote in comp.lang.perl.misc:

    > "Anno Siegel" <-berlin.de> wrote in message
    > news:d6sk51$nio$-Berlin.DE...
    > > Dean Hannotte <> 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
     
    Anno Siegel, May 24, 2005
    #7
  8. Dean Hannotte

    Anno Siegel Guest

    Dean Hannotte <> wrote in comp.lang.perl.misc:

    > "Anno Siegel" <-berlin.de> wrote in message
    > news:d6sk51$nio$-Berlin.DE...
    > > Dean Hannotte <> 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
     
    Anno Siegel, May 24, 2005
    #8
  9. Dean Hannotte

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > "Dean Hannotte" <> wrote in
    > news:MMAke.709$:
    >
    > [ please do not top-post ]
    >
    > > "Anno Siegel" <-berlin.de> wrote in message
    > > news:d6sk51$nio$-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
     
    Anno Siegel, May 24, 2005
    #9
  10. Anno Siegel wrote:

    > Dean Hannotte <> 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
     
    Sherm Pendley, May 24, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    974
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    682
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    504
  4. David Filmer
    Replies:
    19
    Views:
    257
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page