Functional Decomposition
Function Global Scoping
- Functions can appear anywhere in the Perl program. They do not
need to be declared before they are called.
- By default, all variables in a Perl module are global, that is
accessible to both main Perl program and any Perl functions in that
module.
- This includes all variables declared with my in the
main Perl module.
- Example
#!/usr/bin/perl -w
use strict;
my ($GlobalX);
$GlobalX="Hello from Main\n";
print "Before Function, GlobalX = $GlobalX";
aFunction();
print "After Function, GlobalX = $GlobalX";
sub aFunction {
$GlobalX = "Hello from Function\n";
}
==> Before Function, GlobalX = Hello from Main
==> After Function, GlobalX = Hello from Function
Back to Syllabus
Previous: Introduction
Next: Function Local (my) Scoping