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

Some Important Notes on Passing Arguments - Perl

In this fifth part to a six-part article series on subroutines and functions in Perl, you'll learn about lexical variables, and how passing arguments works. 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. Variables and Arguments in Perl
  2. Some Important Notes on Passing Arguments
By: Apress Publishing
Rating: starstarstarstarstar / 0
May 02, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Sometimes we want to pass things other than an ordinary list of scalars, so itís important to understand how passing arguments works.

Function Arguments Passed by Reference

An important thing to know about how Perl passes arguments into functions is that arguments are passed by reference, not by value. This is illustrated in the following example:

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

use strict;

my $var = 10;
print "before: $var\n";
change_var($var);
print "after: $var\n";

sub change_var {
print "in change_var() before: $_[0]\n";
++$_[0];
print "in change_var() after: $_[0]\n";
}

First,$varis assigned 10 and then printed. Then,$varis passed into the functionchange_var(). This function prints the value of$_[0], increments it, then prints it again. The important line of code in this function is

++$_[0];

Since the arguments to the function are passed in through the array@_, to access the zeroth argument of the array we use the syntax$_[0]óthis function prints it, increments it, then prints it again. The important thing to note about this code is that since$varis passed into the function by reference, when$_[0]is incremented, Perl actually increments the argument passed in,$var, from 10 to 11. After the function call, the program then prints the resulting value of$var, which is now 11. Executing the code proves this:

$ perl byref1.pl
before: 10
in change_var() before: 10
in change_var() after: 11
after: 11
$

The fact that Perl passes arguments by reference is not in itself a bad thing, but it can be if you are not expecting it. Having functions modify arguments when we donít want them to can create hard-to-find bugs. There is a very simple way to ensure that our functions donít modify their argumentsósimply copy them intomy() variables as shown in this example:

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

use strict;

my $var = 10;
print "before: $var\n";
change_var($var);
print "after: $var\n";
sub change_var {
my($v) = @_;
# or: my $v = shift;

print "in change_var() before: $v\n";
++$v;
print "in change_var() after: $v\n";
}

The big change here is the first line ofchange_var():

my($v) = @_;

This copies the zeroth element of@_, or$_[0], into$v. As mentioned before and as indicated by the comment, we could have written this as

my $v = shift;

since theshift()function shifts@_by default if invoked within a function (recall also that ifshift()is invoked outside a function it shifts@ARGVby default). Now, since the argument is copied into$v, when we increment it with

++$v;

it antalya escort increments the copy within the function; it does not increment$var. Executing the program proves this:

$ perl byref2.pl
before: 10
in change_var() before: 10
in change_var() after: 11
after: 10
$

Please check back for the conclusion to this article 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: