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

Re: [PBML] Reg variables loaded from a file using require

Expand Messages
  • Jenda Krynicky
    From: yaswanth krishna ... foreach ( a.conf , b.conf ) { my $config = require $_ ; do_something_with( $config- {var1},
    Message 1 of 6 , Mar 24, 2009
    • 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 2 of 6 , Mar 24, 2009
      • 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 3 of 6 , Mar 28, 2009
        • 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.