Perl | Mutable and Immutable parameters
Last Updated :
07 Jun, 2019
A
Perl function or subroutine is a group of statements that together perform a specific task. In every programming language, the user wants to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again. These subroutines contain parameters which define the type and number of arguments that can be passed to the function at the time of function call. These arguments or parameters are used to evaluate the values passed to the function according to the expressions specified in the function. These values are then returned to the specified parameters and are passed to the calling function.
Example:
sub Function1(parameter1, parameter2){ statement; }
Mutable Parameters:
These type of parameters are those whose value can be modified within a function to which they are passed as a parameter. It means that when a parameter is passed to the function using the
caller function, then its value is bound to the parameter in the
called function, which means any changes done to the value in that function will also be reflected in the parameter of the caller function.
Immutable Parameters:
These parameters are of those types whose value can not be modified within a function to which they are passed as parameter. It means that when a parameter is passed to the function using the
caller function, then the subroutine receives a value rather than a variable. Hence, any changes made to the function parameter are not reflected.
By default, the
subroutine parameters in Perl are immutable, which means they cannot be changed within a function and one cannot accidentally modify the arguments of the caller function.
In some languages, this process is known as
'call by value', which means the subroutine being called receives a value rather than the variable, and hence the parameters of the caller function are not modified.
Example:
Perl
#!/usr/bin/perl
# Function Definition
sub Func(Int $variable)
{
# Operation to be performed
$variable /= 2;
}
# Defining a local variable
my $value = 20;
# Function Call with local variable
print Func($value);
Output:
Error: Cannot assign to an immutable value
To change the property of these parameters,
traits are used. With the help of traits, the value of the parameter can be changed within a subroutine.
Traits:
These are the predefined built-in subroutines that when used within a method, modify the behavior of the method when running at compile time. Traits can even be used to change the body of the method or simply tagging the method with metadata.
Traits can be of multiple types depending upon their usage such as:
is cached
trait caches the function's return value automatically, based on the arguments that are being passed to it.
is rw
trait allows the writable accessors to the subroutine parameters.
is copy
trait allows changing the value of the parameter within the subroutine but without changing the argument in the caller function.
Example: Use of
is copy
trait
Perl
#!/usr/bin/perl
# Function Definition using
# 'is copy' trait
sub Func(Int $variable is copy)
{
# Operation to be performed
$variable += 5;
}
# Defining a local variable
my $value = 10;
# Function Call with local variable
print Func($value), "\n";
# Checking if
# $value gets updated or not
print $value;
Output:
15
10
In the above code,
is copy
trait is used because Perl by default, doesn't allow the modification of arguments within a subroutine. This trait allows the program to assign the caller function's parameter value to the parameter of the subroutine being called. But, this trait will only change the value of the argument in the called function.
Example: Use of
is rw
trait
Perl
#!/usr/bin/perl
# Function Definition using
# 'is rw' trait
sub Func(Int $variable is rw)
{
# Operation to be performed
$variable += 5;
}
# Defining a local variable
my $value = 10;
# Function Call with local variable
print Func($value), "\n";
# Checking if
# $value gets updated or not
print $value;
Output:
15
15
In the above code, when
is rw
is used instead of
is copy
trait, the value of the argument passed in the caller function also gets updated.
When
is rw
trait is used, the argument of the called function is bound with the argument of the caller function, so if any change is made in the former value, the latter gets updated immediately. This is because of the process termed as
'call by reference'. Since, both the arguments refer to the same memory location(because of the
is rw
trait). This makes the parameters to be fully mutable.
Similar Reads
Perl | Objects in OOPs Perl is an Objected Oriented, dynamic and interpreter based programming language. In object-oriented programming, we have three main aspects, which are, object, class, and methods. An object is a data type which can be specifically called as an instance of the class to which it belongs. It can be a
6 min read
Perl - Attributes in Object Oriented Programming In Perl, Object Oriented concept is very much based on references and Arrays/Hashes. The few main terms of object-oriented programming with respect to Perl programming are object, class, and method. In Perl, an object is like a reference to a data type that knows about the class it belongs to. The o
6 min read
Perl | Method Overriding in OOPs In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signat
6 min read
Perl | Operators | Set - 2 Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. In Perl, operators symbols will be different for different kind of operands(like scalars and string). Some of the operators already discussed in Per
7 min read
Perl | Automatic String to Number Conversion or Casting Perl has a different way to deal with the operators because here operator defines how the operands will behave but in other programming languages, operands define how an operator behaves. Casting refers to the conversion of a particular variable's data type to another data type. For example, if ther
4 min read