Sunday, November 1, 2020

Perl Split | Perl Function | String Functions in Perl | Perl Scripting | Perl Programming

This is a Perl tutorial on Perl Split and other string functions in Perl from the Perl Tutorials. Perl language has many functions for string processing, that are useful for processing HTML and XML. This tutorial explains some important string functions in Perl. First, view the Perl Split and String Functions tutorial. Then read below.

Let us learn Perl scripting with examples. Here is the first Perl script example with Perl function lc (to convert a string to lower case) and Perl function uc (to convert a string to upper case):

# Perl code example
# First, implement Perl basics.
use strict; # Perl use strict finds any error in the Perl code if found, stops running the code.
use warnings; # Perl use warnings gives a warning if there is any problem with the Perl code.
use feature qw(say); # say is better than Perl print statement because it appends a newline automatically.
# Perl my to declare a variable in Perl
my $blog = "Software Testing Space";
# string functions in Perl
# Perl print variable in lowercase
say "The blog name in lower case is ", lc($blog);
# Perl uppercase or Perl uc string function
say "The blog name in upper case is ", uc($blog);

Here is another Perl scripting example with length Perl function, Perl string operations and index Perl function (to find a substring within a Perl string):

# Perl code example
use strict; # Perl use strict
use warnings; # Perl use warnings
# use strict and use warnings help us write the correct Perl code in Perl programming.
use feature qw(say); # better form of Perl print
# Perl basics are addressed above. Now, write the actual Perl code.
my $name = "Inder P Singh"; # string assignment
say "The length of name, including spaces, is ", length($name);
# Perl expression of string multiplication using x string operator
# . is string the string operator for concatenation
say "The repeated name is ". $name x 3;
my $text = "abcdef";
# Perl find by index; The indexes are 0-based, meaning they start from 0.
say "The string de occurs in text at ", index($text, "de");
 #index string function returns -1 if the substring is missing in the string.
say "The string x occurs in text at ", index($text, "x");

Here is another Perl example with join Perl function, reverse Perl function and Perl split:

# Perl code example
use strict;
use warnings;
use feature qw(say);
# Perl my
my $blog = "Software Testing Space";
# An array variable in Perl can store a list of values.
my @letters = ("a".."j");# An array is declared within parentheses as a range.
# Perl print array; a Perl array is 0-based.
say "The first five letters are ", @letters[0..4]; # Print the first 5 elements of the array.
# join string function returns the joined string with a separator.
say "The joined letters are ", join(":",@letters);
# reverse string function returns the string with the characters in reverse order.
say "The reverse of letters is ",  reverse(@letters);
# Based on a pattern, the split function in Perl returns a list of strings.
# The pattern in Perl split is given within a pair of forward slashes.
# The pattern is a single space character below.
say "The words in the blog are ", split(/ /,$blog); # Perl split
# Split command in Perl with another Perl example, using join with Perl split
say "The comma separated words in the blog are ", join(",",split(/ /,$blog)); # Perl split command

Want to learn Perl more and see the above Perl examples run? Then please view my Perl Split | String Functions in Perl tutorial. Thank you.


Wednesday, October 21, 2020

Perl For Loop - Perl foreach - perl for | Perl Scripting | Perl Programming

This is the Perl tutorial on Perl for loop and Perl foreach from the Perl Tutorials. Perl for loop can be used to run a code block multiple times. Perl foreach loop is a more convenient form of the Perl for loop. First, view the Perl For Foreach tutorial. Then read below.
Perl code can be written in any text editor. The Perl pl script should have the .pl file extension. We can run Perl script within the Command Prompt app. In the Command Prompt window, change directory using cd command to the folder that has our Perl script. Then run the command, perl scriptname.pl 
 
Using Perl for statement, we can run a code block multiple times depending on an initial value, a condition and an action. Here is the first Perl script example with the Perl for loop. This Perl scripting example should print 1, 2 and 3 on different lines.
 
use strict; # Perl use strict finds an error in the code and stops the Perl code if there is an error.
use warnings; # Perl use warnings gives a warning in case of an issue with Perl code.
my $i; # In Perl language, Perl my is used to declare a variable in Perl.
# In this Perl for loop, the initial value of variable i will be 1.
# This Perl for loop will check the condition that i is less than or equal to 5. If true, the Perl for loop will be run once.
# This Perl for loop will increase the value of i by 1 i.e. if i was 1, i will become 2 and so on.
for ($i=1;$i<=5;$i++) {
    # In Perl scripting language, Perl sleep is used to make the script pause for given seconds.
    # if no time is specified, Perl sleep makes the script wait indefinitely.
    sleep 1;
    print "The counter is $i\n"; # Perl print the variable i
    if ($i eq 3){
        # Perl break - last to Perl break out of loop
        last;
    }
}
 
Using Perl foreach statement, we can run a code block multiple times for each value in a list of values. Here is the second Perl script example with the Perl foreach loop. This Perl scripting example should print the first value as 1, the final value as 10. Then it should print all values in the list as 1, 2, 3, 5 and 10.
 
use strict; # In Perl programming, Perl use strict finds an error in the code.
use warnings; # Perl use warnings gives a warning if any issue with Perl code is observed.
# Array variable in Perl can store a list of values.
# The Perl array name is prefixed with @.
# An array is declared within parentheses.
my @scores = (1, 2, 3, 5, 10);
# An array element is accessed with square brackets. A Perl array is 0-based.
print "The first score is $scores[0] \n";
# One of the Perl special variables, $#array gives the last index of the Perl array.
print "The final score is $scores[$#scores]\n";
foreach (@scores){
    # Perl foreach will run for each element of @scores array in Perl
    # $_ is the default special variable automatically set by the Perl foreach loop.
    print "The score is $_\n"; # Perl print array
}

Want to learn Perl more or see the above Perl scripts running? Then please view my Perl For Foreach Tutorial. Thank you.


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 yourscriptfilename.pl
 
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.";
}
else{
    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.