Приглашаем посетить
Гоголь (gogol-lit.ru)

Section 10.8.  Scope of a Package Directive

Previous
Table of Contents
Next

10.8. Scope of a Package Directive

All files start as if we had said package main;.[*] Any package directive remains in effect until the next package directive, unless that package directive is inside a curly-braced scope. In that case, Perl remembers the prior package and restores it when that scope ends. Here's an example:

[*] Perl doesn't make us create an explicit main( ) loop like C. Perl knows that every script needs one, so it gives it to us for free.

package Navigation;

{  # start scope block
  package main;  # now in package main

  sub turn_toward_heading {  # main::turn_toward_heading
    .. code here ..
  }

}  # end scope block

# back to package Navigation

sub turn_toward_port { # Navigation::turn_toward_port
  .. code here ..
}

The current package is lexically scoped, similar to the scope of my variables, narrowed to the innermost-enclosing brace pair or file in which we introduced the package.

Most libraries have only one package declaration at the top of the file. Most programs leave the package as the default main package. However it's nice to know that we can temporarily have a different current package.[Section 10.8.  Scope of a Package Directive]

[Section 10.8.  Scope of a Package Directive] Some names are always in package main regardless of the current package: ARGV, ARGVOUT, ENV, INC, SIG, STDERR, STDIN, and STDOUT. We can always refer to @INC and be assured of getting @main::INC. The punctuation mark variables, such as $_, $2, and $!, are either all lexicals or forced into package main, so when we write $., we never get $Navigation::. by mistake.


Previous
Table of Contents
Next