Perl | Mutable and Immutable parameters

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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;

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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;

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.