Global Declarations (Programming Perl) Book Home Programming PerlSearch this book

4.7. Global Declarations

Subroutine and format declarations are global declarations. No matter where you place them, what they declare is global (it's local to a package, but packages are global to the program, so everything in a package is visible from anywhere). A global declaration can be put anywhere a statement can, but it has no effect on the execution of the primary sequence of statements--the declarations take effect at compile time.

This means you can't conditionally declare subroutines or formats by hiding them from the compiler inside a run-time conditional like an if, since only the interpreter pays attention to those conditions. Subroutine and format declarations (and use and no declarations) are seen by the compiler no matter where they occur.

Global declarations are typically put at the beginning or the end of your program, or off in some other file. However, if you're declaring any lexically scoped variables (see the next section), you'll want to make sure your format or subroutine definition falls within the scope of the variable declarations if you expect it to be able to access those private variables.

Note that we sneakily switched from talking about declarations to definitions. Sometimes it helps to split the definition of the subroutine from its declaration. The only syntactic difference between the two is that the definition supplies a BLOCK containing the code to be executed, while the declaration doesn't. (A subroutine definition acts as its own declaration if no declaration has been seen.) Splitting the definition from the declaration allows you to put the subroutine declaration at the front of the file and the definition at the end (with your lexically scoped variable declarations happily in the middle):

sub count (@);       # Compiler now knows how to call count().
my $x;               # Compiler now knows about lexical variable.
$x = count(3,2,1);   # Compiler can validate function call.
sub count (@) { @_ } # Compiler now knows what count() means.
As this example shows, subroutines don't actually have to be defined before calls to them can be compiled (indeed, the definition can even by delayed until first use, if you use autoloading), but declaring subroutines helps the compiler in various ways and gives you more options in how you can call them.

Declaring a subroutine allows it to be used without parentheses, as if it were a built-in operator, from that point forward in the compilation. (We used parentheses to call count in the last example, but we didn't actually need to.) You can declare a subroutine without defining it just by saying:

sub myname;
$me = myname $0             or die "can't get myname";
A bare declaration like that declares the function to be a list operator, not a unary operator, so be careful to use or there instead of ||. The || operator binds too tightly to use after list operators, though you can always use parentheses around the list operators arguments to turn the list operator back into something that behaves more like a function call. Alternatively, you can use the prototype ($) to turn the subroutine into a unary operator:
sub myname ($);
$me = myname $0             || die "can't get myname";
That now parses as you'd expect, but you still ought to get in the habit of using or in that situation. For more on prototypes, see Chapter 6, "Subroutines".

You do need to define the subroutine at some point, or you'll get an error at run time indicating that you've called an undefined subroutine. Other than defining the subroutine yourself, there are several ways to pull in definitions from elsewhere.

You can load definitions from other files with a simple require statement; this was the best way to load files in Perl 4, but there are two problems with it. First, the other file will typically insert subroutine names into a package (a symbol table) of its own choosing, not your packages. Second, a require happens at run time, so it occurs too late to serve as a declaration in the file invoking the require. There are times, however, when delayed loading is what you want.

A more useful way to pull in declarations and definitions is with the use declaration, which effectively requires the module at compile time (because use counts as a BEGIN block) and then lets you import some of the module's declarations into your own program. Thus use can be considered a kind of global declaration, in that it imports names at compile time into your own (global) package just as if you'd declared them yourself. See the section Section 4.1, "Symbol Tables" in Chapter 10, "Packages", for low-level mechanics on how importation works between packages; Chapter 11, "Modules", for how to set up a module's imports and exports; and Chapter 18, "Compiling" for an explanation of BEGIN and its cousins, CHECK, INIT, and END, which are also global declarations of a sort because they're dealt with at compile time and can have global effects.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.