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

Sharing a Variable Across Scripts

Expand Messages
  • Mike Dillinger
    First of all, I d like to say that this is not how I would do this. I inherited a project at work that was implemented this way. I have a script that requires
    Message 1 of 9 , Oct 15, 2005
      First of all, I'd like to say that this is not how I would do this. I inherited a project at work that was implemented this way.

      I have a script that requires a script that requires another script. So for instance:

      SCRIPT 1:
      ...
      require script2 ;
      ...


      SCRIPT 2:
      ...
      require script3 ;
      ...


      SCRIPT 3:
      ...



      I need to share a variable in script1 with script3. I thought that using "our" would fix it, but it doesn't work. How can I work around this without rewriting the scripts properly?

      Thanks,
      -MikeD
    • Jeff 'japhy' Pinyan
      ... Well, you ve neglected to show us ANY helpful part of the code. I don t know if there are package declarations, I don t know if you ve declared any of the
      Message 2 of 9 , Oct 15, 2005
        On Oct 15, Mike Dillinger said:

        > First of all, I'd like to say that this is not how I would do this. I
        > inherited a project at work that was implemented this way.
        >
        > I have a script that requires a script that requires another script.
        > So for instance:
        >
        > SCRIPT 1:
        > require script2 ;
        >
        > SCRIPT 2:
        > require script3 ;
        >
        > SCRIPT 3:
        >
        > I need to share a variable in script1 with script3. I thought that
        > using "our" would fix it, but it doesn't work.

        Well, you've neglected to show us ANY helpful part of the code. I don't
        know if there are package declarations, I don't know if you've declared
        any of the variables you want to share as lexical, etc.

        This should work:

        # p1.pl
        #!/usr/bin/perl

        use strict;
        use warnings;
        require "p2.pl";

        our $var_from_p3;

        print "P3 gave us $var_from_p3\n";


        # p2.pl
        #!/usr/bin/perl

        use strict;
        use warnings;
        require "p3.pl";

        1;


        # p3.pl
        #!/usr/bin/perl

        use strict;
        use warnings;

        our $var_from_p3 = 100;

        1;


        When I run p1.pl, I get:

        P3 gaves us 100

        > How can I work around this without rewriting the scripts properly?

        Huh? That sounds very silly. Perhaps you meant to write something else?

        --
        Jeff "japhy" Pinyan % How can we ever be the sold short or
        RPI Acacia Brother #734 % the cheated, we who for every service
        http://www.perlmonks.org/ % have long ago been overpaid?
        http://princeton.pm.org/ % -- Meister Eckhart
      • Mike
        ... this. I ... don t ... The our fix indeed does not work. This is on Windows, if that matters. I cannot simply copy the code here because it includes
        Message 3 of 9 , Oct 17, 2005
          --- In perl-beginner@yahoogroups.com, Jeff 'japhy' Pinyan <japhy@p...>
          wrote:
          >
          > On Oct 15, Mike Dillinger said:
          >
          > > First of all, I'd like to say that this is not how I would do
          this. I
          > > inherited a project at work that was implemented this way.
          > >
          > > I have a script that requires a script that requires another script.
          > > So for instance:
          > >
          > > SCRIPT 1:
          > > require script2 ;
          > >
          > > SCRIPT 2:
          > > require script3 ;
          > >
          > > SCRIPT 3:
          > >
          > > I need to share a variable in script1 with script3. I thought that
          > > using "our" would fix it, but it doesn't work.
          >
          > Well, you've neglected to show us ANY helpful part of the code. I
          don't
          > know if there are package declarations, I don't know if you've declared
          > any of the variables you want to share as lexical, etc.
          >
          > This should work:

          The "our" fix indeed does not work. This is on Windows, if that
          matters. I cannot simply copy the code here because it includes
          proprietary information, but I will duplicate the situation.

          *** script1.pl:
          use strict ;
          require script2.pl ;
          our $var1 = 'blah' ;
          print( "$var1\n" ) ; # This works just fine, obviously

          *** script2.pl:
          # strict is not used
          require script3.pl ;
          print( "$var1\n" ) ; # undefined value

          *** script3.pl:
          # strict is not used
          print( "$var1\n" ) ; # undefined value


          > Huh? That sounds very silly. Perhaps you meant to write something
          else?

          Meaning that sharing between scripts is not the desired method. I
          would have written one big module.
        • Jeff 'japhy' Pinyan
          ... It worked perfectly fine for me. ... That code doesn t compile. You mean require script2.pl ; ... You mean require script3.pl ; And OF COURSE $var1 is
          Message 4 of 9 , Oct 17, 2005
            On Oct 17, Mike said:

            > The "our" fix indeed does not work. This is on Windows, if that
            > matters. I cannot simply copy the code here because it includes
            > proprietary information, but I will duplicate the situation.

            It worked perfectly fine for me.

            > *** script1.pl:
            > use strict ;
            > require script2.pl ;
            > our $var1 = 'blah' ;
            > print( "$var1\n" ) ; # This works just fine, obviously

            That code doesn't compile. You mean

            require "script2.pl";

            > *** script2.pl:
            > # strict is not used
            > require script3.pl ;
            > print( "$var1\n" ) ; # undefined value

            You mean

            require "script3.pl";

            And OF COURSE $var1 is undefined in script2.pl. It hasn't been given a
            value yet! You're require()ing script2.pl before script1.pl has a chance
            to set a value for $var1.

            > *** script3.pl:
            > # strict is not used
            > print( "$var1\n" ) ; # undefined value

            Ditto. Same reason.

            --
            Jeff "japhy" Pinyan % How can we ever be the sold short or
            RPI Acacia Brother #734 % the cheated, we who for every service
            http://www.perlmonks.org/ % have long ago been overpaid?
            http://princeton.pm.org/ % -- Meister Eckhart
          • Mike Dillinger
            ... From: Jeff Japhy Pinyan Sent: Monday, October 17, 2005, at 12:16PM PST (GMT -0800) JjP And OF COURSE $var1 is undefined in
            Message 5 of 9 , Oct 17, 2005
              --- Original Message
              From: Jeff 'Japhy' Pinyan <japhy@...>
              Sent: Monday, October 17, 2005, at 12:16PM PST (GMT -0800)

              JjP> And OF COURSE $var1 is undefined in script2.pl. It hasn't been given a
              JjP> value yet! You're require()ing script2.pl before script1.pl has a chance
              JjP> to set a value for $var1.

              There appears to be a communication barrier here. I will look elsewhere for help. Thanks.

              -MikeD
            • Charles K. Clarkson
              ... Just provide a non-working example of the scripts you are trying not to rewrite. I assume you do not wish to send all the scripts to the list, but what you
              Message 6 of 9 , Oct 17, 2005
                Mike Dillinger <> wrote:
                :
                : There appears to be a communication barrier here. I will
                : look elsewhere for help. Thanks.

                Just provide a non-working example of the scripts you are
                trying not to rewrite. I assume you do not wish to send all
                the scripts to the list, but what you have sent is too bare
                for us to aid you.

                This works, for example:

                aa.pl:
                #!/usr/bin/perl

                #use strict;
                #use warnings;

                $var = 'foo';

                __END__

                bb.pl:
                #!/usr/bin/perl

                #use strict;
                #use warnings;

                require "aa.pl";

                print $var;

                __END__


                And so does this:

                aa.pl:
                #!/usr/bin/perl

                #use strict;
                #use warnings;

                $var = 'foo';

                __END__

                #!/usr/bin/perl

                use strict;
                use warnings;

                require "aa.pl";
                our $var;

                print $var;

                __END__


                HTH,

                Charles K. Clarkson
                --
                Mobile Homes Specialist
                254 968-8328
              • Jeff 'japhy' Pinyan
                ... Please listen to me; I am trying to help you. The code you showed the group tries printing a variable BEFORE you assign it a value: *** script1.pl: use
                Message 7 of 9 , Oct 17, 2005
                  On Oct 17, Mike Dillinger said:

                  > --- Original Message
                  > From: Jeff 'Japhy' Pinyan <japhy@...>
                  > Sent: Monday, October 17, 2005, at 12:16PM PST (GMT -0800)
                  >
                  > JjP> And OF COURSE $var1 is undefined in script2.pl. It hasn't been given a
                  > JjP> value yet! You're require()ing script2.pl before script1.pl has a chance
                  > JjP> to set a value for $var1.
                  >
                  > There appears to be a communication barrier here. I will look elsewhere for help. Thanks.

                  Please listen to me; I am trying to help you. The code you showed the
                  group tries printing a variable BEFORE you assign it a value:

                  *** script1.pl:
                  use strict ;
                  require "script2.pl" ;
                  our $var1 = 'blah' ;
                  print( "$var1\n" ) ; # This works just fine, obviously

                  *** script2.pl:
                  # strict is not used
                  require "script3.pl" ;
                  print( "$var1\n" ) ; # undefined value

                  Let's stop RIGHT there. The flow of the program is this:

                  1. (script1.pl) load script2.pl
                  2. (script2.pl) load script3.pl (we'll ignore that)
                  3. (script2.pl) print $var1's value
                  4. (script1.pl) assign 'blah' to $var1
                  5. (script1.pl) print $var1's value

                  Do you see the flaw? You're trying to print $var1's value in script2.pl
                  (and script3.pl) BEFORE you've assigned a value to it.

                  If you simply assign $var1 a value BEFORE you require() script2.pl,
                  everything will be fine.

                  Please do not think I'm launching a personal attack or insulting you. I'm
                  trying to point out a flaw that it appears you've missed.

                  --
                  Jeff "japhy" Pinyan % How can we ever be the sold short or
                  RPI Acacia Brother #734 % the cheated, we who for every service
                  http://www.perlmonks.org/ % have long ago been overpaid?
                  http://princeton.pm.org/ % -- Meister Eckhart
                • Mike Dillinger
                  ... From: Jeff Japhy Pinyan Sent: Monday, October 17, 2005, at 01:50PM PST (GMT -0800) JjP Do you see the flaw? You re trying to print
                  Message 8 of 9 , Oct 17, 2005
                    --- Original Message
                    From: Jeff 'Japhy' Pinyan <japhy@...>
                    Sent: Monday, October 17, 2005, at 01:50PM PST (GMT -0800)

                    JjP> Do you see the flaw? You're trying to print $var1's value in script2.pl
                    JjP> (and script3.pl) BEFORE you've assigned a value to it.

                    JjP> If you simply assign $var1 a value BEFORE you require() script2.pl,
                    JjP> everything will be fine.

                    JjP> Please do not think I'm launching a personal attack or insulting you. I'm
                    JjP> trying to point out a flaw that it appears you've missed.

                    Yes, I see the problem now and thanks.

                    So my question is the following: How can I share a variable from one script to another? The second script will have a function that reads the variable value from the first script, and the catch is that the variable value can change in subsequent readings. The only thing that comes to mind is that I can write the value to a file in the first script, and then write a function in the second script to read the value in each time. If there are other suggestions, they would be highly appreciated.

                    Thanks,
                    -MikeD
                  • Jeff 'japhy' Pinyan
                    ... I assume you mean that the two scripts are running independently of one another? Like, prog1.pl is running, and then you start running prog2.pl and it
                    Message 9 of 9 , Oct 17, 2005
                      On Oct 17, Mike Dillinger said:

                      >So my question is the following: How can I share a variable from one
                      >script to another? The second script will have a function that reads the
                      >variable value from the first script, and the catch is that the variable
                      >value can change in subsequent readings. The only thing that comes to
                      >mind is that I can write the value to a file in the first script, and
                      >then write a function in the second script to read the value in each
                      >time. If there are other suggestions, they would be highly appreciated.

                      I assume you mean that the two scripts are running independently of one
                      another? Like, prog1.pl is running, and then you start running prog2.pl
                      and it needs to get a variable from prog1.pl?

                      Your method of writing and reading is fine. You can also experiment with
                      using "shared memory" to store the variable.

                      --
                      Jeff "japhy" Pinyan % How can we ever be the sold short or
                      RPI Acacia Brother #734 % the cheated, we who for every service
                      http://www.perlmonks.org/ % have long ago been overpaid?
                      http://princeton.pm.org/ % -- Meister Eckhart
                    Your message has been successfully submitted and would be delivered to recipients shortly.