Home arrow Perl Programming arrow Invoking Perl Subroutines and Functions

Invoking Perl Subroutines and Functions

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

Invoking a Subroutine

The conventional way to invoke a function is to follow the function name with parentheses. This invokes the example_subroutine()function:

example_subroutine();

If the function takes arguments (more on passing arguments later in this chapter), then drop them within the parentheses:

example_subroutine('Perl is', 'my favorite', $language);

Letís look at a complete example. Itís traditional for programs to tell you their version and name either when they start up or when you ask them with a special option. Itís also convenient to put the code that prints this information into a subroutine to get it out of the way. Letís take a recognizable program and update it for this traditional practice.

Hereís what we started with, version 1:

#!/usr/bin/perl -w

print "Hello, world!\n";

And here it is with strict mode turned on and version information:

#!/usr/bin/perl -w
# hello2.pl

use strict;

sub version {
print "Beginning Perl's "Hello, world." version 2.0\n";
}

my $option = shift; # defaults to shifting @ARGV

version() if $option eq "-v" or $option eq "--version";

print "Hello, world.\n";

Now, weíre starting to look like a real utility:

$ perl hello2.pl -v
Beginning Perl's "Hello, world." version 2.0 Hello, world.
$

The first thing we see inhello2.plis the definition of theversion()function:

sub version {
print "Beginning Perl's "Hello, world." version 2.0\n";
}

Itís a simple block of code that calls theprint()function. It didnít have toóit could have done anything. Any code thatís valid in the main program is valid inside a subroutine, including calling other functions.

We call this block the body of the subroutine, just like we had the body of a loop; similarly, it stretches from the open curly brace after the subroutine name to the matching closing curly brace.

Now that weíve defined it, we can use it. We invoke the function withversion(), and Perl runs that block of code, albeit with the proviso weíve added the right flag on the command line.

version() if $option eq "-v" or $option eq "--version";

When itís finished executingversion(), it comes back and carries on with the next statement:

print "Hello, world.\n";

No doubt version 3 will address the warnings that Perl gives if you call this program without appending-vor
--versionto its name.



 
 
>>> 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: