Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Perl | Passing Complex Parameters to a Subroutine
Next article icon

Perl | Mutable and Immutable parameters

Last Updated : 07 Jun, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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.

Next Article
Perl | Passing Complex Parameters to a Subroutine

A

Abhinav96
Improve
Article Tags :
  • Perl
  • Perl-function

Similar Reads

  • Perl | Number and its Types
    A Number in Perl is a mathematical object used to count, measure, and perform various mathematical operations. A notational symbol that represents a number is termed as a numeral. These numerals, in addition to their use in mathematical operations, are also used for ordering(in the form of serial nu
    4 min read
  • Perl | Passing Complex Parameters to a Subroutine
    Prerequisite: Perl | Subroutines or Functions 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 a function or subroutine so that there will be no need
    4 min read
  • Perl List and its Types
    Introduction to Lists A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol wher
    4 min read
  • 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 | Math::BigInt->config() method
    Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators. config() method of Math::BigInt module is used to get the information about the configuration of the Perl module. Syntax: Math::BigInt->config() Parameter: None Ret
    1 min read
  • Perl | Methods in OOPs
    Methods are used to access and modify the data of an object. These are the entities which are invoked with the use of objects of a class or a package itself. Methods are basically a subroutine in Perl, there is no special identity of a method. Syntax of a method is the same as that of a subroutine.
    5 min read
  • Perl - Creating a Hash from an Array
    Hashing is the process of converting a given key into another value. A hash function is used to generate the new value (called hash) according to a mathematical algorithm. A Perl hash is defined by key-value pairs. Perl stores elements of a hash in such an optimal way that you can look up its values
    4 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences