Home arrow Perl Programming arrow Page 2 - Lists and Arguments in Perl

Default Argument Values - Perl

In this conclusion to a six-part article series on subroutines and functions in Perl, you'll learn more about lists and arrays, and take a look at default argument values. This article was excerpted from chapter six of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

  1. Lists and Arguments in Perl
  2. Default Argument Values
  3. Named Parameters
By: Apress Publishing
Rating: starstarstarstarstar / 0
May 08, 2012

print this article



One thing thatís occasionally useful is the ability to give the arguments for your subroutine a default value. That is, give the argument a value to use in the subroutine if one is not specified when the subroutine is called. This is very easily done with the ||operator (the logical or operator).

||has a very special feature: it returns the last thing it evaluates. So, for instance, if we say$a = 3 || 5, then$awill be set to 3. Because 3 is a true value, the or operator has no need to examine anything else, and so 3 is the last thing the||evaluates. If, however, we say$a = 0 || 5, then$awill be set to 5; 0 is not a true value, so the operator looks at the next operand, 5, which is the last thing it evaluates. This behavior is called short circuiting.

Hence, anything we get from@_that doesnít have a true value can be given a default with the||operator. We can create subroutines with a flexible number of parameters and have Perl fill in the blanks for us:

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

use strict;

log_warning("Klingons on the starboard bow", "Stardate 60030.2");
log_warning("/earth is 99% full, please delete more people");

sub log_warning {
my $message = shift || "Something's wrong";
my $time = shift || localtime; # Default to now.
print "[$time] $message\n";

$ perl defaults.pl
[Stardate 60030.2] Klingons on the starboard bow
[Wed May 5 04:07:50 2004] /earth is 99% full, please delete more people
[Wed May 5 04:07:51 2004] Something's wrong

One by-product of specifying defaults for parameters is the opportunity to use those parameters as flags. Your subroutine can then alter its functionality based on the number of arguments passed to it.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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