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

RE: [PBML] GLOBAL VARIABLES

Expand Messages
  • Charles K. Clarkson
    ... A global variable as I used the term with Robin might be defined as any variable in a subroutine which was not local in scope to that subroutine. For
    Message 1 of 12 , Apr 28, 2004
    • 0 Attachment
      Robin <robin@...> wrote:

      : "Luinrandir Hernsen" <Luinrandir@...>
      :
      :: Help me understand
      ::
      :: A GLOBAL VARIABLE is one declared at the top of the code,
      :: and a local variable is one declared inside a loop? a
      :: sub{}? a for{}?
      :
      : A global variable does not have to be declared at the top
      : of the code, although for stylistic purposes it's better
      : to define them at the top of the code, it can be defined at
      : the bottom of the code. Charles K. Clarkson actually told
      : me not to use these because when you give the code to some
      : other programmer and they try to modify it. Globals are
      : generally defined the "our" function. The other kind of
      : variable is local and is defined with my, it doesn't have
      : to be inside of a code block.


      A global variable as I used the term with Robin might
      be defined as any variable in a subroutine which was not
      local in scope to that subroutine.

      For example, in this subroutine $blogfile is a global
      variable. It was defined as a configuration variable at the
      beginning of the script (file scope).

      sub getposts {
      return () unless open FH, $blogfile;

      my @posts = <FH> || ();
      close FH;

      return @posts;
      }



      There is an exception to this. Static variables:

      print counter(), "\n" foreach 1 .. 10;

      BEGIN {
      my $contrived_counter = 2000;

      sub counter {
      return $contrived_counter++;
      }
      }

      Here $contrived_counter is local to the block above and
      technically a global (to this sub). It retains its value
      between calls to counter() and is not available to the rest
      of the program. BEGIN{} is needed to initially set
      $contrived_counter to 2000.


      This points to one way of creating a globals without
      exposing them to the whole program and allowing read-only
      access anywhere:

      BEGIN {
      my %global = (
      blog_file => 'blog.txt',
      count_file => 'count.txt',
      split_string => '<--->',
      header_file => 'blogheader.txt',
      footer_file => 'blogfooter.txt',
      );
      sub config {
      return $global{ $_[0] };
      }
      }

      #
      # No serviceable parts below!
      #


      sub getposts {
      return () unless open FH, config( 'blog_file' );

      my @posts = <FH> || ();
      close FH;

      return @posts;
      }


      Note that strict does allow us to use type globs as
      file handles. These are also globals as I defined globals
      above. (They are available throughout the program.) Perl
      5.6.1 allows us to use scalars in place of type globs,
      but the idiom hasn't caught on.


      open FH, $file or die qq|Cannot open "$file": $!|;

      open my $fh, $file or die qq|Cannot open "$file": $!|;

      $fh is a lexical scalar. FH is global. It is in the
      symbol table The second example won't work in older perl
      versions.


      sub getposts {
      return () unless open my $fh, config( 'blog_file' );

      my @posts = <$fh> || ();
      return @posts;
      }

      ($fh closes implicitly as it loses scope.)


      HTH,

      Charles K. Clarkson
      --
      Mobile Homes Specialist
      254 968-8328


      NOTE: This is a Windows version of getposts() which leaves out flocking.
    Your message has been successfully submitted and would be delivered to recipients shortly.