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.
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:
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.
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:
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.
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:
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 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:
The following output appears after executing the script if 50 is taken as input:
Conclusion
The uses of the “my” keyword inside and outside of different types of blocks are shown in the examples of this tutorial.