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: }