Sunday, October 11, 2020

Perl | Perl Scripting - Perl If - Perl If Not Statement | Perl Programming

This is the Perl tutorial on Perl if and Perl if not statements from the Perl Tutorials. Perl language is a general-purpose and open source programming language. Perl language is useful for regular expression parsing and text processing (like HTML, XML etc.). Perl language is efficient, simple to learn and use and has many libraries. There is use of Perl scripting language in tasks such as system administration, web based development etc. We can use Perl scripting language to write from simple scripts to complex applications. Perl coding language has many features. View the Perl If tutorial. Then read below.
Installing perl on Windows is straight-forward. I have explained Strawberry Perl with Setup Wizard screenshots in the initial part of the above Perl tutorial. The Strawberry Perl Setup Wizard should automatically update the Path system variable in Windows 10.
You can write Perl code using any text editor. The Perl pl script has to be saved with .pl file extension. Running Perl script can be done by running the Command Prompt app in Windows 10. In the Command Prompt window, change directory using cd command to the folder that has your pl script. Then run the command, perl
Since this tutorial is regarding Perl scripting for beginners, let us start with Perl basics. Here is the first Perl script example. Other Perl programming examples are below.

# This is a Perl script, meaning it has at least one statement in the Perl scripting language.
# Each Perl scripting statement ends with a semi-colon.
# Comments in Perl start with a # sign.
use strict; # Perl use strict finds an error in the code and stops running the Perl code.
use warnings; # Perl use warnings gives a warning in case of a problem.
# A string is some text. A string has to have a pair of double quotes around it.
# Or a string literal has to have a pair of single quotes around it.
print "Welcome to Perl Training"; # Perl print statement

If you have run the above Perl example, let us continue to learn Perl. Here is another Perl example to show more Perl basics.

use strict; # Perl use strict finds any error in the Perl code.
use warnings; # Perl use warnings gives a warning, if needed.
my $name = "John";  # Perl my keyword is used to declare a variable, $name
my $number = 100; # Perl my declares the Perl variable, $number
print "My name is ", $name, "\n"; # Perl print variable value, \n takes the cursor to the next line
print "The half of number is ", $number/2; # Perl print Perl expression

Perl if statement is used to conditionally run a code block. The condition in Perl if is put within parentheses. A Perl code block is one or more Perl statements that are enclosed within curly braces. The Perl condition is followed by the Perl if code block. Optionally, another Perl condition can be given after elsif in the Perl if statement. The Perl elsif has it's own code block. The remaining condition is handled by the Perl else. The Perl else has it's code block after it. Here is a Perl script example for Perl compare string. Run the following Perl example with $string2 variable having mulitple values like "abc", "def" and "ABC".
use strict; # Perl use strict
use warnings; # Perl use warnings
my $string1 = "abc"; # Declare the Perl variable, $string1 to store a Perl string.
my $string2 = "abc"; # Declare the Perl variable, $string2, to store the second string.
if ($string1 eq $string2){ # The condition is $string equals $string2.
    print "The two strings are the same.";
elsif ($string1 lt $string2){ # Perl elsif condition is $string1 less than $string2.
    # The variable name can even be written inside the string to print the variable's value.
    print "$string1 is smaller than $string2";
else {# A code block starts with the left curly brace, {
    print "$string1 is greater than $string2";
} # A code block ends with the right curly brace, }

Perl if not statement is also used to conditionally run a code block. There is a not operator, !, before the condition. The not operator reverses the condition, meaning the condition if true becomes false and vice versa. Here is another Perl code example for Perl compare string in Perl programming. Perl compare string is case sensitive. Run the following Perl example with other values also for the second Perl string, like "a" and "Abc".
use strict;
use warnings;
if (!("abc" eq "abc")){
    print "The two strings are different.";
    print "The two strings are the same.";

Want to learn Perl more? Want to see the above Perl script example running? Then please view my Perl If - Perl If Not tutorial. Thank you.

1 comment:

  1. Thanks for sharing informative and valuable information,keep on updating these types of informative things. Here in right information about of digital marketing services.