BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

my keyword – Local and global variables in Perl

Last Updated: May 15, 2017 by Chaitanya Singh | Filed Under: Perl

When we talk about local and global variables, we are in fact talking about scope of variable.

Local variable

Local variable scope is local, its existence lies between those two curly braces( generally referred as block of code), outside of that block this variable doesn’t exist. These variables also known as lexical variables.

These variables can be used in any block of code, it can be in any control structure block like if, if-else, etc or any loop block like for, while, do-while etc. or any subroutine’s block , it can even be present in an anonymous block. For example –

#!/usr/bin/perl
use strict;
use warnings;
if (1<2)
{
    my $age = 29;
    print "$age\n";     # prints 29
}

I know that this example doesn’t make any sense but my point here is to show you, how a local variable looks in a program. In the above program variable $age is declared inside if block, thus this variable is local to this block only. If we try to access this variable outside of if body, we would get an error. Let’s see this in action, run the following program.

#!/usr/bin/perl
use strict;
use warnings;
if (1<2)
{
    my $age = 29;
    print "$age\n";     # prints 29
}
print "$age\n";

Output:

Global symbol "$age" requires explicit package name at demo.pl line 9.
Execution of demo.pl aborted due to compilation errors.

Note:
1) Local variables are declared using my keyword as you could see in the above program.
2) Since the scope of local variables are limited to the block, you can use same name local variables in different blocks without any conflicts.
3) The variable declared using my keyword, at the beginning of the programs right after pragmas would be accessible throughout the program. For example, the variable $age in this program is accessible though out the program.

#!/usr/bin/perl
use strict;
use warnings;
my $age =29;
if (1<2)
{
    print "$age\n";     
}
print "$age\n";

Output:

29
29

Few important things regarding my keyword:
Till now, we have learnt that my keyword is used to declare local variables, lets see few important point regarding my keyword.

1) When declaring multiple variables using my keyword, you must use parentheses, otherwise it would declare only one local variable. For example,

my $num1, $num2;  # This would not declare $num2.
my ($num1, $num2); # This is the correct way. It declares both

2) Apart from variables, you can also declare local arrays and local hashes using my keyword.

For example, this code declares local array @friends.

my @friends;

Global variables:

This doesn’t need any introduction as we are using them in our programs since when we started the perl tutorial. All the variables that are directly used without the declaration(using my keyword) are accessible from every part of the program. For example,

#!/usr/bin/perl
use warnings;
if (1<2)
{
    $age =29;     #no declaration using my keyword
    print "$age\n";     
}
print "$age\n";  #accessible outside the block

Output:

29
29

Note: In this program, we have removed the use strict pragma as it forces us to declare all the variables using my keyword before using them.

❮ PreviousNext ❯

Top Related Articles:

  1. Scalars in Perl
  2. Installing Perl on Windows, Mac, Linux and Unix
  3. Perl Variables
  4. Perl Tutorial for beginners
  5. Data Types in Perl

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap