Home arrow Perl Programming arrow Page 2 - Invoking Perl Subroutines and Functions

Order of Declaration and Invoking Functions - Perl

In this second part of a six-part article series on subroutines and functions in Perl, you will learn how to invoke a subroutine and a function. This article was excerpted from chapter six of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Invoking Perl Subroutines and Functions
  2. Order of Declaration and Invoking Functions
By: Apress Publishing
Rating: starstarstarstarstar / 0
February 29, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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();
get_input();
process_input();
output();

That would then be followed, presumably, by something like this:

sub setup {
print "This is some program, version 0.1\n";
print "Opening files...\n";
open_files();
print "Opening network connections...\n";
open_network();
print "Ready!\n";
}

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
# subdecl.pl

use strict;

setup;

sub setup {
print "This is some program, version 0.1\n";
}

$ perl subdec1.pl
Bareword "setup" not allowed while "strict subs" in use at subdecl.pl line 6.
Execution of subdecl.pl aborted due to compilation errors.
$

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;
sub get_input;
sub process_input;
sub output;
sub open_files;
sub open_network;
...

From now on, we can happily use the subroutines without the parentheses:

setup;
get_input;
process_input;
output;

sub setup {
print "This is some program, version 0.1\n";
print "Opening files...\n";
open_files;
print "Opening network connections...\n";
open_network;
print "Ready!\n";
}

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;
&get_input;
&process_input;
&output;

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.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: