Perl

“My” Keyword in Perl

The “my” keyword is used in Perl to declare the variable of the same name outside and inside of any block in the Perl script. This keyword is mainly used to define the local variables inside the block of code in Perl. If the variable of the same name is defined with the “my” keyword globally and locally, the value of the global variable will be replaced by the local variable. The different uses of the “my” keyword in Perl are shown in this tutorial.

Different Examples of “My” Keyword

The purposes of using the “my” keyword inside a block and outside a block are shown in this part of the tutorial using multiple examples.

Example 1: Declare a Variable without the “My” Keyword

Create a Perl file with the following script that declares the variables without using the “my” keyword. Here, a variable named “$language” is declared before the block and this variable is updated inside the “if” block. This variable is printed before the block, inside the block, and after the block.

#!/usr/bin/perl

use warnings;
#Define a string  variable
$language = 'PERL';
#Print variable
print "The value of the variable before the block is $language\n";
$start = 1;
if ($start)
{
    #Update the variable
    $language = 'C++';
    #Print the updated value
    print "The value of the variable inside the block is $language\n";
}
#Print the updated value outside the block
print "The value of the variable after the block is $language\n";

Output:

The following output appears after executing the script. Here, the content of the “$language” variable is updated inside the block:

p1

Example 2: Declare a Variable with the “My” Keyword

Create a Perl file with the following script that declares the variables with the “my” keyword. Here, a variable named “$number” is declared before and inside the “if” block with the “my” keyword. The variable inside the “if” block does not update the variable outside the “if” block. So, the variable inside the block works like a local variable for using the “my” keyword.

#!/usr/bin/perl
use strict;
use warnings;

#Define a number variable
my $number = 150;
#Print the variable
print "The number before the block is $number\n";
my $start = 1;
if ($start)
{
    #Update the variable
    my $number = $number + 50;
    #Print the updated value
    print "The number inside the block is $number\n";
}
#Print the updated value outside the block
print "The number after the block is $number\n";

Output:

The following output appears after executing the script. According to the output, the value of the “$number” variable is the same before and after the block:

p2

Example 3: Declare a Variable with the “My” Keyword Inside a Function

Create a Perl file with the following script that declares a variable with the same name outside and inside a function using the “my” keyword. Here, a variable named “$str”

is declared before and inside the function with the “my” keyword. The variable inside the function does not update the variable outside the function like in the previous example because it works as a local variable. But if the “my” keyword is omitted for the “$str” variable inside the function, the value of the global variable is updated.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

#Declare variable outside the function
my $str = "Perl script";
say "The value of the variable before calling the function is '$str'.";

#Call the function
message();

say "The value of the variable after calling the function is '$str'.";

#Declare the function
sub message
{
    #Declare same variable inside the function
    my $str = "Python script";
    say "The value of the variable inside the function is '$str'.";
}

Output:

The following output appears after executing the script. According to the output, the value of the “$str” variable is the same before and after calling the function:

p3

Example 4: Declare a Variable with the “My” Keyword Inside the “For” Loop

The method of using the “my” keyword inside the “for” loop is shown in this example. The counter variable of the “for” loop named “$i” is declared with the “my” keyword. The following script takes a number from the user and checks whether the number is between 1 to 15 or not:

use strict;

use warnings;

print "Enter a number: ";

my $number = <>;

chomp($number);

my $last=0;

#Declare for loop to iterate 15 times

for (my $i = 1; $i <= 15; $i++)

{
    #Check the input value
    if(int($number) == $i)
    {
         print "$number is found.\n";
         last;
    }
    #Assign the current value of $i
    $last = $i;
}

#Check how many times the loop was iterated
if($last == 15)
{
    print "$number does not exist between 1 to 15.\n";
}

Output:

The following output appears after executing the script if 8 is taken as input:

p4-1

The following output appears after executing the script if 50 is taken as input:

p4-2

Conclusion

The uses of the “my” keyword inside and outside of different types of blocks are shown in the examples of this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.