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

Re: Global variables - when to use

Expand Messages
  • acummingsus
    ... Most of what I learned about scope I learned from Randal Schwartz s writings. His Learning Perl book and Linux Magazine Perl articles are great, super.
    Message 1 of 5 , Aug 2, 2005
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "johnbr123zzz"
      <johnbr123zzz@y...> wrote:
      > I have a fairly long script and want to use a hash to collect data.
      > This hash will need to be accessed by a number of subroutines, each
      > of which will add or change data and return the modified hash.
      >
      > Is the only way to do this by declaring the hash globally. I've read
      > a lot about trying to minimise the use of global variables but am
      > still grappling with the concept of passing by reference. Thanks for
      > any input.

      Most of what I learned about scope I learned from Randal Schwartz's
      writings. His "Learning Perl" book and Linux Magazine Perl articles
      are great, super.

      Mind you, I only know so much. I'm a beginner. Or, maybe
      beginner-intermediate (the experts can laugh at the roughness of my
      code). But the code showed me a bit about scope when I ran it.

      My perception at this point portrays that you let it have the scope
      that it needs in order to get the job done. But, never let it have
      more scope (more or more outward scope, outward block) than what is
      needed.

      AFAIK passing by reference to sub routine, *as far as scope is
      concerned*, works the same as my "experimenting with scope" ie can
      access at and/or outward from any given block (block level).

      The safety of (use/advantage of scope) is in the avoiding of a
      variable getting clobbered/overwritten by another variable that's of
      matching variable name.

      They say that the bigger the program, the more important is the use of
      good scope practices. (troubleshooting , code maintenance ease, etc.)

      Hopefully, if I have any portion of this wrong, I'll be corrected.

      #!/usr/bin/perl -w
      use strict;

      # Experimenting with scope is the title
      # as it is it runs and we print what we can when we can from where we can
      # due to scope there are three print lines currently commented out
      # from inside, inner, (block) can access therein and/or outward thereof
      # but once something goes out of scope
      # ie a block is left for a block of another, importantly a more
      outward, level
      # then cannot reverse access so to speak. By reverse access I mean an
      attempt to,
      # from outer try to access something declared/assigned_and_also_of
      more inner scope.

      # un comment one of the commented print lines. Doing so makes it, from
      outward,
      # tries to access what was declared/assigned at more inner level
      (level, levels of scope).
      # And since the
      # more inner level is out of scope from where tries to access from, an
      error ensues.

      # most global (no block)
      my @outermost_array = qw/ Joe Wally Fred /;
      while (<DATA>) { # outermost block

      if ( /^Wind:/ ) { # middle block
      chomp;
      # @parsed_wind_fields array 1st declared/assigned here
      my @parsed_wind_fields = split;
      print "\nnow in middle block\n";
      print "\@outermost_array: @outermost_array\n";
      print "the middle block \@parsed_wind_fields: @parsed_wind_fields\n\n";
      # try to access @innerm array here at middle block
      # print "\@innerm_wind_fields: @innerm_wind_fields\n";

      if ( $parsed_wind_fields[0] =~ /^Wind:/ ) { # innermost block
      # @innerm array 1st declared/assigned here
      my @innerm_wind_fields = "@parsed_wind_fields";
      print "\nnow in innermost block\n";
      print "\@outermost_array: @outermost_array\n";
      print "the middle block \@parsed_wind_fields: @parsed_wind_fields\n";
      print "\@innerm_wind_fields: @innerm_wind_fields\n\n";
      } # end of innermost block

      } # end of middle block

      } # end of outermost block

      print "now in most global ie no block\n";
      print "\@outermost_array: @outermost_array\n";
      # try to access @innerm (innermost block) array here at most global point
      # print "\@innerm_wind_fields: @innerm_wind_fields\n";
      # try to access @parsed_wind_fields (middle block) array here at most
      global point
      # print "the middle block \@parsed_wind_fields: @parsed_wind_fields\n";
      print "\n";

      __DATA__
      Sacramento, Sacramento Executive Airport, CA, United States (KSAC)
      38-30-25N 121-29-42W 11M
      Jul 31, 2005 - 11:53 PM EDT / 2005.11.01 0353 UTC
      Wind: from the SSW (200 degrees) at 6 MPH (5 KT):0
      Visibility: 10 mile(s):0
      Sky conditions: clear
      Temperature: 80.1 F (26.7 C)
      Dew Point: 54.0 F (12.2 C)
      Relative Humidity: 40%
      Pressure (altimeter): 29.86 in. Hg (1011 hPa)
      ob: KSAC 010353Z 20005KT 10SM CLR 27/12 A2986 RMK AO2 SLP110 T02670122
      cycle: 4
      # end_of_DATA

      Alan.
    • Charles K. Clarkson
      ... Use an object. Most are hash based and passing them by reference is pretty automatic. You also can create customized subroutines to ease data changes. HTH,
      Message 2 of 5 , Aug 2, 2005
      • 0 Attachment
        johnbr123zzz <> wrote:

        : I have a fairly long script and want to use a hash to collect
        : data. This hash will need to be accessed by a number of
        : subroutines, each of which will add or change data and return
        : the modified hash.
        :
        : Is the only way to do this by declaring the hash globally. I've
        : read a lot about trying to minimise the use of global variables
        : but am still grappling with the concept of passing by reference.
        : Thanks for any input.

        Use an object. Most are hash based and passing them by
        reference is pretty automatic. You also can create customized
        subroutines to ease data changes.


        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        254 968-8328
      • Jeff 'japhy' Pinyan
        ... Well, the preferred way would be, as you ve said, to use references. There s plenty of standard documentation to help you learn about them, and a good
        Message 3 of 5 , Aug 2, 2005
        • 0 Attachment
          On Aug 2, johnbr123zzz said:

          > I have a fairly long script and want to use a hash to collect data.
          > This hash will need to be accessed by a number of subroutines, each
          > of which will add or change data and return the modified hash.
          >
          > Is the only way to do this by declaring the hash globally. I've read
          > a lot about trying to minimise the use of global variables but am
          > still grappling with the concept of passing by reference. Thanks for
          > any input.

          Well, the preferred way would be, as you've said, to use references.
          There's plenty of standard documentation to help you learn about them,
          and a good starting place is 'perldoc perlreftut'.

          Here's a brief example:

          my %hash = ();

          add_key(\%hash); # \%hash creates a reference to %hash

          print $hash{foo};

          sub add_key {
          my $href = $_[0];
          $href->{foo} = 10; # $href->{foo} <==> $hash{foo}
          }

          --
          Jeff "japhy" Pinyan % How can we ever be the sold short or
          RPI Acacia Brother #734 % the cheated, we who for every service
          http://japhy.perlmonk.org/ % have long ago been overpaid?
          http://www.perlmonks.org/ % -- Meister Eckhart
        • johnbr123zzz
          Thanks for all your replies. Your advice (and examples) has helped to clarify what I need to do now and what would be nice to do in the future. Regards John
          Message 4 of 5 , Aug 3, 2005
          • 0 Attachment
            Thanks for all your replies. Your advice (and examples) has helped to
            clarify what I need to do now and what would be nice to do in the
            future.

            Regards
            John

            --- In perl-beginner@yahoogroups.com, Jeff 'japhy' Pinyan
            <japhy@p...> wrote:
            > On Aug 2, johnbr123zzz said:
            >
            > > I have a fairly long script and want to use a hash to collect
            data.
            > > This hash will need to be accessed by a number of subroutines,
            each
            > > of which will add or change data and return the modified hash.
            > >
            > > Is the only way to do this by declaring the hash globally. I've
            read
            > > a lot about trying to minimise the use of global variables but am
            > > still grappling with the concept of passing by reference. Thanks
            for
            > > any input.
            >
            > Well, the preferred way would be, as you've said, to use
            references.
            > There's plenty of standard documentation to help you learn about
            them,
            > and a good starting place is 'perldoc perlreftut'.
            >
            > Here's a brief example:
            >
            > my %hash = ();
            >
            > add_key(\%hash); # \%hash creates a reference to %hash
            >
            > print $hash{foo};
            >
            > sub add_key {
            > my $href = $_[0];
            > $href->{foo} = 10; # $href->{foo} <==> $hash{foo}
            > }
            >
            > --
            > Jeff "japhy" Pinyan % How can we ever be the sold short or
            > RPI Acacia Brother #734 % the cheated, we who for every service
            > http://japhy.perlmonk.org/ % have long ago been overpaid?
            > http://www.perlmonks.org/ % -- Meister Eckhart
          Your message has been successfully submitted and would be delivered to recipients shortly.