Loading ...
Sorry, an error occurred while loading the content.

Reg variables loaded from a file using require

Expand Messages
  • yaswanth krishna
    Hi, I am using a script where it works out of loop and loads different configs everytime from config files. I am loading that configs using require
    Message 1 of 6 , Mar 23 10:10 PM
    • 0 Attachment
      Hi,

      I am using a script where it works out of loop and loads different configs
      everytime from config files. I am loading that configs using 'require'
      everytime.

      But the problem is, variables loaded once are not getting cleared, because
      of which if the same variable is not defined in 2nd config file, script is
      still using the value it require'd from 1st config file. Can anyone tell me
      how to clear the variables loaded from 1st config file before moving to 2nd
      one..

      flow something looks like this:
      --------------------------
      foreach ('a.conf', 'b.conf') {

      require '$_';
      # Here I am having some code using those vars

      }
      --------------------------
      #a.conf

      $var1 = 0;
      $var2 = 1;
      --------------------------

      #b.conf

      $var1 = 10;
      --------------------------
      Though $var2 is not there in b.conf, script is still using the value loaded
      from a.conf.

      I can manually clear every var using 'undef'. But is there any other way to
      handle this problem to clear all variables loaded from a file.

      Thanks,
      -Yaswanth


      [Non-text portions of this message have been removed]
    • Charles K. Clarkson
      ... That doesn t sound like a good plan. You are importing unknown variables and routines into your symbol table. That is not a usually a good perl programming
      Message 2 of 6 , Mar 24 2:40 AM
      • 0 Attachment
        yaswanth krishna wrote:

        > I am using a script where it works out of loop and loads different
        > configs everytime from config files. I am loading that configs using
        > 'require' everytime.

        That doesn't sound like a good plan. You are importing unknown variables
        and routines into your symbol table. That is not a usually a good perl
        programming practice.


        > But the problem is, variables loaded once are not getting cleared,
        > because of which if the same variable is not defined in 2nd config
        > file, script is still using the value it require'd from 1st config
        > file. Can anyone tell me how to clear the variables loaded from 1st
        > config file before moving to 2nd one.

        They shouldn't be getting cleared automatically. They are in the
        "main::" symbol table now. (I think that's the name. I am too lazy to go
        look it up.) You would have to remove them from the symbol table. I
        really don't need to know how to do that and it sounds like a lot of
        trouble for a configuration file.

        Give us more info about the problem you are trying to solve. There is a
        better perl solution than the one you are using.


        HTH,

        Charles Clarkson
        --
        Mobile Home Investor
        Free Market Advocate
        Programmer

        Stephenville, TX
        http://www.clarksonenergyhomes.com/wordpress/about/
        http://twitter.com/CharlesClarkson
        +1 (254) 968-8328
      • yaswanth krishna
        ... As mentioned in the earlier example, I need the script to run in different modes. For every mode of invocation, I am taking mode specific configs from
        Message 3 of 6 , Mar 24 4:01 AM
        • 0 Attachment
          >Give us more info about the problem you are trying to solve. There is a
          >better perl solution than the one you are using.

          As mentioned in the earlier example, I need the script to run in different
          modes. For every mode of invocation, I am taking mode specific configs from
          respective config files and proceeding. And I prepared every config file in
          the way perl understands and I am simply require'ing that file.

          Hope it explains my requirement.

          ~Yaswanth


          On Tue, Mar 24, 2009 at 3:10 PM, Charles K. Clarkson
          <cclarkson@...>wrote:

          > yaswanth krishna wrote:
          >
          > > I am using a script where it works out of loop and loads different
          > > configs everytime from config files. I am loading that configs using
          > > 'require' everytime.
          >
          > That doesn't sound like a good plan. You are importing unknown variables
          > and routines into your symbol table. That is not a usually a good perl
          > programming practice.
          >
          > > But the problem is, variables loaded once are not getting cleared,
          > > because of which if the same variable is not defined in 2nd config
          > > file, script is still using the value it require'd from 1st config
          > > file. Can anyone tell me how to clear the variables loaded from 1st
          > > config file before moving to 2nd one.
          >
          > They shouldn't be getting cleared automatically. They are in the
          > "main::" symbol table now. (I think that's the name. I am too lazy to go
          > look it up.) You would have to remove them from the symbol table. I
          > really don't need to know how to do that and it sounds like a lot of
          > trouble for a configuration file.
          >
          > Give us more info about the problem you are trying to solve. There is a
          > better perl solution than the one you are using.
          >
          > HTH,
          >
          > Charles Clarkson
          > --
          > Mobile Home Investor
          > Free Market Advocate
          > Programmer
          >
          > Stephenville, TX
          > http://www.clarksonenergyhomes.com/wordpress/about/
          > http://twitter.com/CharlesClarkson
          > +1 (254) 968-8328
          >
          >


          [Non-text portions of this message have been removed]
        • Jenda Krynicky
          From: yaswanth krishna ... foreach ( a.conf , b.conf ) { my $config = require $_ ; do_something_with( $config- {var1},
          Message 4 of 6 , Mar 24 7:38 AM
          • 0 Attachment
            From: yaswanth krishna <yaswanth.krishna@...>
            > I am using a script where it works out of loop and loads different configs
            > everytime from config files. I am loading that configs using 'require'
            > everytime.
            >
            > But the problem is, variables loaded once are not getting cleared, because
            > of which if the same variable is not defined in 2nd config file, script is
            > still using the value it require'd from 1st config file. Can anyone tell me
            > how to clear the variables loaded from 1st config file before moving to 2nd
            > one..

            Do not load several variables, load one hash with the config data:

            --------------------------
            foreach ('a.conf', 'b.conf') {

            my $config = require '$_';
            do_something_with( $config->{var1}, $config->{var2};
            ...
            }
            --------------------------
            #a.conf
            {
            var1 => 0,
            var2 => 1,
            }
            --------------------------

            #b.conf
            {
            var1 => 10,
            }
            --------------------------

            This way the data from one config will never affect the data from
            another.

            Jenda
            ===== Jenda@... === http://Jenda.Krynicky.cz =====
            When it comes to wine, women and song, wizards are allowed
            to get drunk and croon as much as they like.
            -- Terry Pratchett in Sourcery
          • merlyn@stonehenge.com
            ... yaswanth As mentioned in the earlier example, I need the script to run in yaswanth different modes. For every mode of invocation, I am taking mode
            Message 5 of 6 , Mar 24 7:57 AM
            • 0 Attachment
              >>>>> "yaswanth" == yaswanth krishna <yaswanth.krishna@...> writes:

              yaswanth> As mentioned in the earlier example, I need the script to run in
              yaswanth> different modes. For every mode of invocation, I am taking mode
              yaswanth> specific configs from respective config files and proceeding. And I
              yaswanth> prepared every config file in the way perl understands and I am
              yaswanth> simply require'ing that file.

              yaswanth> Hope it explains my requirement.

              I think that qualifies far more as "describes my implementation" than
              "explains my requirement". Your solution is only slightly better than the
              horrible practice of duplicating the entire script and editing the variations,
              but falls far short of much better solutions.

              Perl is a full-featured programming language. Learn to use it properly.

              --
              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
              See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
            • igotux igotux
              Were you able to solve this ? If so, how... /Tux On Tue, Mar 24, 2009 at 10:40 AM, yaswanth krishna
              Message 6 of 6 , Mar 28 11:33 PM
              • 0 Attachment
                Were you able to solve this ? If so, how...

                /Tux
                On Tue, Mar 24, 2009 at 10:40 AM, yaswanth krishna <
                yaswanth.krishna@...> wrote:

                > Hi,
                >
                > I am using a script where it works out of loop and loads different configs
                > everytime from config files. I am loading that configs using 'require'
                > everytime.
                >
                > But the problem is, variables loaded once are not getting cleared, because
                > of which if the same variable is not defined in 2nd config file, script is
                > still using the value it require'd from 1st config file. Can anyone tell me
                > how to clear the variables loaded from 1st config file before moving to 2nd
                > one..
                >
                > flow something looks like this:
                > --------------------------
                > foreach ('a.conf', 'b.conf') {
                >
                > require '$_';
                > # Here I am having some code using those vars
                >
                > }
                > --------------------------
                > #a.conf
                >
                > $var1 = 0;
                > $var2 = 1;
                > --------------------------
                >
                > #b.conf
                >
                > $var1 = 10;
                > --------------------------
                > Though $var2 is not there in b.conf, script is still using the value loaded
                > from a.conf.
                >
                > I can manually clear every var using 'undef'. But is there any other way to
                > handle this problem to clear all variables loaded from a file.
                >
                > Thanks,
                > -Yaswanth
                >
                > [Non-text portions of this message have been removed]
                >
                >
                >


                [Non-text portions of this message have been removed]
              Your message has been successfully submitted and would be delivered to recipients shortly.