Problem with "require" and multiple modules.

Discussion in 'Perl' started by Don, Dec 9, 2003.

  1. Don

    Don Guest

    I have a set of modules that all have the same interface, and package name:
    _______________________________
    package dataSourcePackage;

    # Initialize global variables

    $hashOne{'uniquekey1'} = "value1";
    $hashOne{'uniquekey2'} = "value1";

    sub requiredMethod1()
    {
    ....
    }
    ....

    return 1;
    _________________________________
    The processing in each module is different, but the interface MUST remain
    the same. I have scripts which call each module based on an input to the
    script:
    _________________________________
    ../processDataSource dataSource1

    (Which will do the following:)

    require "/path/to/mods/ARGV[0]"; # I do have error testing this is just a
    simplified version

    @datapoints = dataSourcePackage::getPoints(5);

    # Process points.

    _________________________________

    My problem is I now have to write a script which runs multiple packages
    during the same execution, and sometimes will call the same package twice.
    The issue I am having is that most of the modules have hardcoded data in the
    section I have marked as "Initialize global data", and contain hashes with
    the same name. So when I do something like:
    --------------------------------
    foreach $datasource (@datasources)
    {
    require "/path/to/mods/$datasource";
    dataSourcePackage::syncPoints(-1);
    }
    --------------------------
    dataSource2 will try to access dataSource1 points, which causes runtime
    errors. Is there a way to destroy a package after it is loaded, so that the
    next time I load a dataSourcePackage, it will start fresh? (NOTE: there are
    a VERY large number of these dataSourcePackages, and it wouldn't be easy to
    modify ALL the packages, any help would be greatly appreciated.)

    Thanks,
    Don.
    Don, Dec 9, 2003
    #1
    1. Advertising

  2. Don

    Guest

    "Don" <> wrote in message news:<1OaBb.196964$>...
    > I have a set of modules that all have the same interface, and package name:


    Bad idea.

    > My problem is I now have to write a script which runs multiple packages
    > during the same execution, and sometimes will call the same package twice.
    > The issue I am having is that most of the modules have hardcoded data in the
    > section I have marked as "Initialize global data", and contain hashes with
    > the same name. So when I do something like:
    > --------------------------------
    > foreach $datasource (@datasources)
    > {
    > require "/path/to/mods/$datasource";
    > dataSourcePackage::syncPoints(-1);
    > }
    > --------------------------
    > dataSource2 will try to access dataSource1 points, which causes runtime
    > errors. Is there a way to destroy a package after it is loaded, so that the
    > next time I load a dataSourcePackage, it will start fresh?


    This is FAQ: "How do I clear a package?"

    You'll also need to delete the entry in %INC.

    > (NOTE: there are
    > a VERY large number of these dataSourcePackages, and it wouldn't be easy to
    > modify ALL the packages, any help would be greatly appreciated.)


    If the packages don't rely too many other modules at compile time you
    could load each in it's own Safe compartment.

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.
    , Dec 10, 2003
    #2
    1. Advertising

  3. "Don" <> wrote in message news:<1OaBb.196964$>...
    > I have a set of modules that all have the same interface, and package name:
    > _______________________________
    > package dataSourcePackage;
    >
    > # Initialize global variables
    >
    > $hashOne{'uniquekey1'} = "value1";
    > $hashOne{'uniquekey2'} = "value1";
    >
    > sub requiredMethod1()
    > {
    > ...
    > }
    > ...
    >
    > return 1;
    > _________________________________
    > The processing in each module is different, but the interface MUST remain
    > the same. I have scripts which call each module based on an input to the
    > script:
    > _________________________________
    > ./processDataSource dataSource1
    >
    > (Which will do the following:)
    >
    > require "/path/to/mods/ARGV[0]"; # I do have error testing this is just a
    > simplified version
    >
    > @datapoints = dataSourcePackage::getPoints(5);
    >
    > # Process points.
    >
    > _________________________________
    >
    > My problem is I now have to write a script which runs multiple packages
    > during the same execution, and sometimes will call the same package twice.
    > The issue I am having is that most of the modules have hardcoded data in the
    > section I have marked as "Initialize global data", and contain hashes with
    > the same name. So when I do something like:
    > --------------------------------
    > foreach $datasource (@datasources)
    > {
    > require "/path/to/mods/$datasource";
    > dataSourcePackage::syncPoints(-1);
    > }
    > --------------------------
    > dataSource2 will try to access dataSource1 points, which causes runtime
    > errors. Is there a way to destroy a package after it is loaded, so that the
    > next time I load a dataSourcePackage, it will start fresh? (NOTE: there are
    > a VERY large number of these dataSourcePackages, and it wouldn't be easy to
    > modify ALL the packages, any help would be greatly appreciated.)
    >
    > Thanks,
    > Don.


    to enforce reoloading of the packages try to delete from the %INC hash :
    delete $INC{"/path/to/mods/$datasource");

    Ralph
    Ralph Henneberg, Dec 10, 2003
    #3
    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. nrolland
    Replies:
    1
    Views:
    101
    George Ogata
    Dec 4, 2006
  2. Fernando Cacciola
    Replies:
    3
    Views:
    79
    Xavier Noria
    Nov 6, 2007
  3. Chad M.
    Replies:
    2
    Views:
    133
    Chad M.
    Mar 27, 2009
  4. buildmorelines

    use require and loading modules

    buildmorelines, Sep 29, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    113
    Brian McCauley
    Sep 30, 2004
  5. Gunnar Hjalmarsson

    Loading Modules with "require"

    Gunnar Hjalmarsson, Dec 27, 2004, in forum: Perl Misc
    Replies:
    12
    Views:
    143
    Juha Laiho
    Dec 28, 2004
Loading...

Share This Page