{

Learn free perl tutorials


Check File and directory exists in Perl code example

January 9, 2023 ·  2 min read

This tutorial explains checking file or folder that exists in Perl programming with code examples. It is very helpful before accessing files or folders in Perl. Check File exists or not in Perl use the -e existence operator that checks file path exists or not. use this option in conditional statements if and print the statement. $file="c://work/abc.pdf"; if( -e $file){ print("File exists"); } The same way can be rewritten using a single short syntax....


Different ways to convert String to number in Perl with code examples

January 9, 2023 ·  3 min read

This tutorial explains multiple ways to convert a String to a Number in Perl with Code examples. Use String variable by append with a number o using the + operator.For example, "123"+0 returns number 123. use the Int type constructor. For example, int("111") returns 111 sprintf function, Assign the result sprintf("%d", "98"); and variable holds number 98 How to Convert String to Number in Perl? using string append with + Symbol The string contains a group of characters and letters, enclosed in double or single quotes in Perl....


Find Array size in Perl with Code example

January 9, 2023 ·  2 min read

The array contains a list of elements unordered. Sometimes, you need to find the number of elements i.e size. This post talks about multiple ways to know the size of an array in Perl with code examples. Perl Array length examples Perl allows us to use an array in a list and scalar context. In List Content, Array returns the list of elements. Scalar Context, Array return size of an element...


How can I check if a Perl array contains a particular value?

January 9, 2023 ·  2 min read

This tutorial explains and Check If Element Exists in an Array of Perl With Code Examples. we’ll take a look at a few different examples of Check If Element Exists In Array Perl How do you check if an element in an array exists in Perl? There are multiple ways we can check using linear search With linear Search, arrays are iterated sequentially and every element is checked for an element that exists or not....


How do I compare two strings or numbers in Perl with an example

January 9, 2023 ·  2 min read

This tutorial explains How to check given two strings are equal or not in Perl Check whether Two Strings are equal or not in Perl There are multiple ways we can compare whether strings are equal or not. use Compare(cmp) operator cmp compares two strings, and returns 0,-1,1 operand1 cmp operand2 Returns 0 if operand1 and operand2 are equal Returns -1 if operand1 is less than or equal to operand2 Returns 1 if operand1 is greater than or equal to operand2...


How do you round a floating point number in Perl with code example

January 9, 2023 ·  2 min read

This tutorial explains multiple ways to round floating numbers in Perl. Perl Floating number round example There are multiple ways we can round a float number Floating numbers can be rounded in two types. ceiling: a rounding number to a number greater than the current number. For example round of 2.6 is 3. Floor: rounding number to a number less than the current number. For example round of 2.6 is 2....


How to break out of a loop in Perl with code example

January 9, 2023 ·  2 min read

This tutorial explains multiple ways to break and exit from a loop in Perl loops. Many programming languages provide break keywords to exit from a loop. Perl has the last to exit from the loop. Perl provides multiple ways to loop iteration using for, foreach, while, and do while syntax. for loop with last to exit from the loop Created an array of strings initialized in Perl Iterated array using for loop Each iteration does the following things Checks current string is three and exits from the loop Print the current string for each iteration Here is foreach last example...


How to call Shell Script from Perl code example

January 9, 2023 ·  2 min read

Multiple ways to call a shell script or a command from Perl code. Using System function with command and arguments, example system(“sh test.sh”,), control returned to called code with output and errors. using the exec function, It breaks the execution once the command executes. How to call shell commands or scripts from Perl with code examples using the System function The System function executes the command or scripts and returns the control to the next line of execution Syntax:...


How to Check String is empty or not in Perl with code example

January 9, 2023 ·  2 min read

This tutorial explains Multiple ways to check whether a given string is empty or not with Perl code examples. using string comparison operator, eq and ne, $str eq "" or numeric comparison operator $str=="" Another way using length function. length($str) == 0 Perl String Check Empty using string comparison operators Perl provides the following String numeric operators. eq is an equal operator, Check given string is matched for another string....


How to get Local and UTC date and time in Perl with code example

January 9, 2023 ·  2 min read

Multiple ways to get Unix timestamp in Perl using Perl’s Inbuilt time function, which returns a long number.time() outputs the long number time function in cpan Time::HiRes module. What is the Current Epoch(Unix) time in Perl Epoch time is the total number of seconds, the ie long number represents that already passed from 1 January 1970 UTC. the total number of seconds per day is 24\* 60\*60 seconds. Epoch time returns the long number between the current time and 1 January 1970....


How to get Local and UTC date and time in Perl with code example

January 9, 2023 ·  1 min read

This tutorial explains about how to get Current Date and time in Local and UTC How to get Current Date and time in UTC format in Perl using DateTime use strict; use warnings; use DateTime; my $dt = DateTime->now; print "$dt" How to get Current Date and time in Local System time in Perl This is an example of getting date and time of a Local System. use strict; use warnings; use DateTime; my $dt = Localtime(); print "$dt" ...


How to Iterate key and values in Hash Perl script with an example

January 9, 2023 ·  2 min read

This tutorial explains how to pass command line arguments to a Perl script file with code examples. Many ways to iterate hash objects in Perl. using a while loop with each hash variable in a conditional expression, returns the next pair of items in a hash, and Iterates each pair of elements, printingkey and value pairs. using for each loop with each hash variable To iterate only keys, use keys with a hash variable, that returns an array of keys, iterate using for loop To iterate only values, use values with hash variable, that returns an array of values, iterate using for loop How to iterate keys and values in Perl Hash using while loop each hashvarible returns the next key and value of elements of the hash object....


How to pass command line arguments to Perl script with an example

January 9, 2023 ·  2 min read

This tutorial explains how to pass command line arguments to a perl script file with code examples. Command line arguments are the way to pass parameters to the perl execution script file. For example, the script.pl "args" "args" command runs the script.pl file and pass two arguments to the perl program. Perl Command Line Arguments Example First, way using the special variable @ARGV. Perl provides an inbuilt special variable @ARGV that stores command line arguments as an array....


How to Print hash key and value in Perl with an example

January 9, 2023 ·  3 min read

This short tutorial shows you how to print the hash key and value in Per with examples. Hash is a data type in Perl that contains key and value pairs. hash can be declared with its values as given below my $emp = { "one" => 1, "two" => 2, "three" => 3, }; Perl Print Hash Object Hash Object printed to console in multiple ways. print used to print the values....


How to reverse a String and List in Perl| Reverse function

January 9, 2023 ·  2 min read

This tutorial shows you how to reverse a string and list in Perl. Perl reverse function does two things In Scalar Context, It reverses the string into reverse order In List Context, It reverses the list into the reverse list Perl Reverse Function example reverse function reverse variable data. Syntax reverse(variable); Reverse the data in the variable and returns it Here is an example my $str="Hello"; $reverseStr = reverse($str); print "$reverseStr"; Output:...


How to trim whitespaces in a string, leading and trailing

January 9, 2023 ·  2 min read

This tutorial shows multiple ways to remove whitespace characters from a given string in Perl. using the String::Util module trim function to remove leading and trailing spaces. Example trim(" abc “) returns abc. using String pattern matching operator =~ use the Text::Trim module trim function How to trim leading and trailing whitespace characters in Perl string The String::Util module contains trim functions and removes the lead and trail whitespace characters....


Multiple ways iterate an array in Perl with an example

January 9, 2023 ·  2 min read

Perl arrays are a group of ordered values assigned to a variable. values are scalar types in Perl. The array can be declared with the @ sign @numbers = (11, 22, 33,44,55,66); A single element can be read using an array variable with the dollar($) sign with an index. print "$numbers[0]\n"; This tutorial explains multiple ways to iterate an array in Perl. Iterate an array using for loop Array is declared and assigned with a list of numbers used for loop and do iteration, assign an iterated element to $item Print each element using the print statement Here is an example...


Multiple ways to Convert Hexadecimal to/from Number Perl

January 9, 2023 ·  2 min read

Learn Perl conversion of Hexa string and numbers with code examples Converted Hexa String to number using the hex function. For example, hex(“0x1”) returns 1. Convert Number to Hexa decimal using the springf function with format %02X Perl Hexadecimal Number System Hexadecimal number is also called a Hexa number which contains 16 digits i.e numbers starting from 0,1,2,3,4,5,6,7,8,9,A, B,C,D,E,F. It is based on Base 16. i.e Hexa number system. So Hexa Decimal contains Decimal Numbers,0-9 (base 10) and Extra 6 alphabets - A to F...


Multiple ways to get executable script file name and path in Perl with an example

January 9, 2023 ·  2 min read

This tutorial explains how to get the name of the file and the full path of a Perl script execution file. How to get the file name and full path to a Perl script that is executing? There are multiple ways to get the following things Name of the Perl script file Full path of the Perl script file use the Cwd module The Cwd module provides the abs_path function that gets the absolute path of a file...


Multiple ways to print an array in Perl with an example

January 9, 2023 ·  2 min read

This tutorial explains multiple ways to print an array with examples using print with an array variable using join data:dumper JSON::XS map function Data::Printer Easy way to print an array with formatted in Perl with examples An easy way to print a variable in Perl using print statements. if print contains an array variable, It prints all elements with space separation. @numbers = (1, 2, 3,4,5,6); print "@numbers\n"; Output:...


Multiple ways to remove duplicate elements from an array in Perl with code example

January 9, 2023 ·  2 min read

This tutorial explains multiple ways to remove duplicate elements in Perl Array How to remove duplicate elements from an array in Perl To remove elements from an array in Perl, Please follow the below steps. using List MoreUtils uniq function import uniq from the List::MoreUtils library call the uniq method with an array of duplicate values Returns array by removing duplicate values Here is an example use List::MoreUtils qw(uniq); my @numbers = (1,2,3,1,1,4,3,2); my @result = uniq(@numbers); print "@numbers\n"; Output:...


Perl Array - Create empty size array, assign zero or space with code examples

January 9, 2023 ·  2 min read

This tutorial explains multiple ways create an empty-sized array with a length of zero create and initialize an array with elements containing zero create and initialize an array with elements containing space How to create an empty array with zero elements in Perl The empty array can be created in two ways Declare an Array variable the variable is declared using the my keyword, followed by a variable prefixed with @....


Subscribe
You'll get a notification every time a post gets published here.