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