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

Re: [PBML] What is, (or 'How to') a 'fully qualified' variable?

Expand Messages
  • Charles K. Clarkson
    Dave Benware ... OMG, Armegeddon is near, Bompa is using strict. The sky is falling! The sky is falling! The sky is falling! ...
    Message 1 of 1 , Feb 3, 2002
    • 0 Attachment
      "Dave Benware" <bompa@...>

      : I've never used 'use strict' and I still see no reason to use it.
      : However, due to the great amount of talk about it, I thought I'd
      : at least read up on it and gain an understanding of it's purpose
      : and the advantages of using it.

      OMG, Armegeddon is near, Bompa is using strict. The sky is
      falling! The sky is falling! The sky is falling!

      : (I guess it follows that I haven't been using "our", "local", or
      : "my" either and I'm reading those docs now also.)
      :
      :
      : The quote below seems to give four ways to declare and access a variable
      without
      : triggering a compile-time error from 'use strict':
      : 1. our,
      : 2. use vars,
      : 3. my,
      : 4. fully qualified.
      :
      : I'm missing, (maybe looking right past), what is meant by "fully qualified".

      When you use a variable in a perl program without using 'my' you
      normally do this:

      $tree = 1;

      When doing this perl places $tree in the symbol table. Any variable
      in the symbol table can be "fully qualified" by mentioning its package.
      If we don't give a package name to our script perl assigns the package
      a name of: 'main'.

      To fully qualify

      $tree

      we would write:

      $main::tree

      We can also shorten this with:

      $::tree

      If you would like to view the symbol table ( %:: ). Use this:

      #!/usr/bin/perl

      my $my = 1;
      $::temp = 1;
      printf "% 25s => $::{$_}\n", $_ for sort keys %::;

      Notice that $::temp is listed, but $private is not.
      Only the items in the symbol table are accessable by other
      scripts.


      When you use "our $temp; " or "use vars '$temp'; ", perl
      allows you to use $temp without qualifying it.

      Notice how this places one entry in the symbol table for $temp.

      BEGIN {
      my %was;
      @was{keys %::} = (1) x keys %::;
      $was{DATA} = 1;
      sub was { $was{$_[0]} }
      }

      our $temp;
      #use vars '$temp';

      for ( sort keys %:: ) {
      printf "% 25s => $::{$_}\n", $_ unless was $_;
      }

      Notice how 'use vars' imports more variable than just temp
      into the table. This is one reason why 'our' is preferable to

      BEGIN {
      my %was;
      @was{keys %::} = (1) x keys %::;
      $was{DATA} = 1;
      sub was { $was{$_[0]} }
      }

      #our $temp;
      use vars '$temp';

      for ( sort keys %:: ) {
      printf "% 25s => $::{$_}\n", $_ unless was $_;
      }


      HTH,
      Charles K. Clarkson
      Clarkson Energy Homes, Inc.
      254 968-8328


      Whose cruel idea was it for the word 'Lisp' to have an 'S' in it?
    Your message has been successfully submitted and would be delivered to recipients shortly.