Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Scoping Study Aid program

by zeno (Friar)
on Jan 15, 2001 at 23:39 UTC ( [id://52021]=perlcraft: print w/replies, xml ) Need Help??

   1: I had a lot of trouble understanding Perl scoping.  After
   2: reading the FAQs, etc, I decided to try writing a program
   3: trying out everything.  Now I think I understand it.
   4: 
   5: This code consists of a program and a perl module.  They
   6: should be in the same directory.  Run the program.  It
   7: will show how I tried to refer to different declared
   8: variables and subs.
   9: 
  10: This program uses undeclared package variables
  11: (terminology correct?), 'my' variables, and 'our'
  12: variables.  It doesn't use 'local' variables, though I
  13: might change it.  And it also demonstrates using AUTOLOAD
  14: subs to react to calls to undefined subs.
  15: 
  16: I really want this to be useful.  So if you see something
  17: I screwed up, or terminology which is wrong, etc, please
  18: tell me.  Thanks! -tim allen
  19: 
  20: (I) THE PROGRAM (scoping.pl)
  21: #!/usr/bin/perl
  22: # (note: this program will generate a warning if the -w
  23: # command line parameter is used.  This is on purpose
  24: # to show that 'my' causes variables to be lexically scoped
  25: # to their package. -tim)
  26: # Program: scoping.pl
  27: # Purpose: to try out the various ways of referring to variables
  28: # and subs in a package, specifically using "our"
  29: # Author: tim allen
  30: # Restrictions: 1) I don't "use strict" here on purpose so I
  31: #  can have variables not defined as 'my' or 'our'.
  32: #  2) I don't declare anything 'local' -- maybe later.
  33: 
  34: # The AUTOLOAD sub is special: it catches undefined
  35: # references to subs.  Programming Perl, p. 297
  36: sub AUTOLOAD {
  37:   our $AUTOLOAD;
  38:   warn "$AUTOLOAD NOT DEFINED IN scoping.pl. (AUTOLOAD)\n";
  39:   return undef;
  40: }
  41: 
  42: sub here_sub {
  43:   my $in_val = shift;
  44:   return "received $in_val in here_sub";
  45: }
  46: 
  47: use Our_package;
  48: 
  49: # DEFINITIONS
  50: # define a package variable for *this* package
  51: $package_variable = "defined without 'my' or 'our' in main";
  52: # ... and a lexically scoped variable as well
  53: my $my_variable = "defined with 'my' in main";
  54: # END OF DEFINITIONS
  55: 
  56: print "VARIABLES IN *THIS* PACKAGE\n";
  57: print "1) \$package_variable=$package_variable\n";
  58: print "2) \$my_variable=$my_variable\n";
  59: 
  60: print "\nVARIABLE DECLARED IN *OTHER* PACKAGE\n";
  61: print "1) \$our_variable = $our_variable\n" if (defined $our_variable);
  62: print "2) \$our_variable NOT DEFINED\n"   if not (defined $our_variable);
  63: print "3) \$Our_package::our_variable = $Our_package::our_variable\n";
  64: print "4) \$Our_package::my_variable NOT DEFINED\n"
  65:   if not (defined $Our_package::my_variable);
  66: 
  67: print "\nPACKAGE VARIABLE IN OUTSIDE PACKAGE\n";
  68: print "1) \$package_variable=$package_variable (WRONG)\n";
  69: print "2) \$Our_package::package_variable=$Our_package::package_variable\n";
  70: 
  71: print "\nCALL SUBROUTINES\n";
  72: print "1) Our_package::our_sub(4) = ".Our_package::our_sub(4)."\n";
  73: print "2) here_sub(4) = ".here_sub(4)."\n";
  74: print "3) here_sub($my_variable) = ".here_sub($my_variable)."\n";
  75: 
  76: if ($_ = our_sub()) {
  77:   print "our_sub() = ".$_."\n"; 
  78: }
  79: 
  80: if ($_ = Our_package::our_foo($my_variable)) {
  81:   print "Our_package::our_foo($my_variable) = ".$_."\n";
  82: }
  83: 
  84: (II) AND NOW THE PERL MODULE (Our_package.pm)
  85: package Our_package;
  86: our $our_variable = "defined with 'our' in Our_package";
  87: $package_variable = "defined without 'my' or 'our' in Our_package";
  88: my $my_variable = "defined with 'my' in Our_package";
  89: 
  90: sub AUTOLOAD {
  91:   our $AUTOLOAD;
  92:   warn "$AUTOLOAD NOT DEFINED IN Our_package.pm.(Our_package::AUTOLOAD)\n";
  93:   return undef;
  94: }
  95: 
  96: sub our_sub {
  97:   my $in_val = $_[0];
  98:   return "defined in Our_package (received $in_val)";
  99: }

Replies are listed 'Best First'.
Re: Scoping Study Aid program
by a (Friar) on Jan 16, 2001 at 09:56 UTC
    Scoping does make my brain hurt: I get:
    Name "Our_package::my_variable" used only once: possible typo at scope.pl line 41.
    for
    print "4) \$Our_package::my_variable NOT DEFINED\n" if not (defined $Our_package::my_variable);
    Shouldn't $Our_package::my_variable be something? I'm looking for a typo but ...

    a

      I get:
      Name "Our_package::my_variable" used only once: possible
      typo at scope.pl line 41.
      for

      print "4) \$Our_package::my_variable NOT DEFINED\n" if not (defined $Our_package::my_variable);
      Shouldn't $Our_package::my_variable be something?
      I'm looking for a typo but ...

      You're right! I get the warning, too, but I let this happen on purpose. This generates a warning because I purposely refer to a variable declared 'my my_variable' in the other package. Since it's declared with 'my', it is (let's see if I get this right) lexically scoped only to the other package, so the calling program can't see it. As I understand it, a variable with lexical scoping means that it only exists within a particular textual area of code, in this case, the package "Our_package". I guess a solution to this would be for me to change the "#!/usr/bin/perl -w" to "#!/usr/bin/perl". Or maybe print a disclaimer about the warning. The point is, the user should learn from this that a 'my' variable in a package is not visible from outside that package.
      I went ahead and changed the code and included a disclaimer. Enjoy! :) -tim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (None)
    As of 2024-04-25 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found