Perl Programming Page 2 - Invoking Perl Subroutines and Functions |
Normally, functions are called using the parens as we did in the preceding program: version() We can also call them without the parentheses if the function is defined before it is invoked: version If we just call our subroutines by name without parentheses, as we just saw, we’re forced to declare them before we use them. This may not sound like much of a limitation, but there are times when we’ll want to declare our subroutines after the main part of the program; in fact, that’s the usual way to structure a program. This is because when you open up the file in your editor, you can see what’s going on right there at the top of the file, without having to scroll through a bunch of definitions first. Take the extreme example at the beginning of this chapter: #!/usr/bin/perl -w use strict; setup(); That would then be followed, presumably, by something like this: sub setup { sub open_files { Tip That’s far easier to understand than trawling through a pile of subroutines before getting to the four lines that constitute our main program. In order to get this to work, we need to provide hints to Perl as to what we’re doing: that’s why the preceding calls to subroutines have a pair of parentheses:setup(),open_files(), and so on. This helps to tell Perl that it should be looking for a subroutine somewhere instead of referring to another type of variable. What happens if we don’t do this? #!/usr/bin/perl -w use strict; setup; sub setup { $ perl subdec1.pl Perl didn’t know what we meant at the time and complained. So, to tell it we’re talking about a subroutine, we use parentheses, just like when we want to disambiguate the parameters to a function likeprint(). There’s another way we can tell Perl that we’re going to refer to a subroutine, and that’s to provide a forward definition—also known as predeclaring the subroutine. This means “We’re not going to define this right now, but look out for it later.” We do this by just sayingsub NAME;, and note that this does require a semicolon at the end. Here’s another way of writing the preceding example: #!/usr/bin/perl -w use strict; sub setup; From now on, we can happily use the subroutines without the parentheses: setup; sub setup { sub open_files { Alternatively, you can ask Perl to provide the forward declarations for you. If we sayuse subs (...), we can provide a list of subroutine names to be predeclared: #!/usr/bin/perl -w use strict; use subs qw(setup get_input process_input output pen_files open_network); ... You may also see yet another way of calling subroutines: &setup; This was popular in the days of Perl 4, and we’ll see later why the ampersand is important. For the time being, think of the ampersand as being the “type symbol” for subroutines. In this book we will stick to calling our functions with parentheses to clearly indicate that we are invoking a function. Sometimes, the more clarity the better. Please check back for the next part of the series.
blog comments powered by Disqus |
|
|
|
|
|
|
|