Updated April 10, 2023
Introduction to Perl one liner
One liner simply states that which can be done via only one line of code. By using one liner we can write effective code in one line that can be executed very fast. One liner is a single line code in Perl, this one liner includes and perform many operations such as replacing of text, spacing, deleting, calculation, manipulation in files, process updating in chunk, conversion and many more to come. One liner makes this easy to execute and readable as well. We can write this complex functioning in single line using one liner in Perl.
How Perl one liner Works?
As we already saw that one liner is used to support the single line piece of code that can perform operations in one go. We can write one liner for so many things in Perl like deletion, updating, replacing certain text from the string and so on. This can be very well suited with the files or long sequence of string which we want to replace with a specific text then this one-liner can be used efficiently.
Now we will see some of the samples of one-liner and understand its working in detail.
1. To count the lines number from a file
Suppose we have one requirement where we want to count the number of lines present in the file. Perl comes up with a keyword or we can say symbol which is used to get the number of line present in the file. For this we have to use ‘$.’ dollar sign followed by the ‘dot (.)’ in Perl. This symbol or syntax will return us the number of lines in a particular file.
Let’s see the syntax to get the number of line from file.
Syntax:
"$. $_"
2. Generate sequence
By using the one liner in Perl we can generate a sequence of input that can be anything like integers from 1 to 10, alphabets from a to z etc. For now, we have one syntax to generate a to z alphabets in Perl. For this we can use a..z this is a viable in perl, also after generation we can hold this inputs in any variable.
Let’s see below syntax to get the sequence of alphabets in perl using one liner.
Syntax:
'print map { (a..z)'
In the above single liner we are trying to print the map that will contain the sequence of alphabets. This will be fast and as you can see easy to read and understand as well.
3. Get random alpha bate
We can also write a single liner to get the random character or a form the map. For this we have ‘rand’ keyword in Perl. Inside this we can pass the number elements present inside the map, so it will return the random alphabet from the present element.
Syntax:
'[rand num_of_elemets]'
In the above lines of syntax, we can use ‘rand’ keyword with the number of elements, also this has to be wrap inside the square brackets.
4. To repeat any operation
By using the one liner we can repeat our operation any number of times. For this we have to mentioned the starting number and ending number, between these two we have to specify the ‘..’ to make it work.
Let’s see its syntax for its usage:
Syntax:
1..10
In the above line of code we are trying to repeat the operation for 10 times. The last one represents the number of times we want it to repeat.
5. Replace text
By the use of this one liner we can replace the text present in the file with some new text if it is found. So what will Perl does it will open the file for us search for the text that need to be replaced and return us the updated file after the modification. This whole process code we can write into a single line by using one liner available in Perl.
Let’s see its syntax of its usage:
Syntax:
s/find/replace/flags
This is the basic syntax for replace one liner as the Perl documentation. At the place of find we can write the string or text that we want Perl to be find in respective file. At the place of ‘replace’ we can write the string or text that we want to see after the new modification has been done by the Perl one liner. At the end we can mentioned our file which we want to get modified.
6. Find decimal number
We also have one liner which is used to find the decimal number present in the file. For this we can use ‘unpack’ keyword.
Advantages of Perl one liner
As we have so many one liner available in Perl.
Let’s have a look at the advantages of using one liner:
- One liner performs so many operations at the same time.
- This optimize our code.
- Make it easy to maintain, read and understand by the developers.
- By the use of one liner we can perform so many things like deleting, replacing, modification in the existing file, repetition of creation operation to any number of count, to get the random value form the list of elements and so on.
- Most of the operation are done by the Perl internally that we do not know for instance, we want to make some changes into he existing file then we just make the one liner for it, but Perl will open the file make the changes close the file and return us the result. So there are so many things which are going internally which we do not know.
Example of Perl one liner
Given below is the example mentioned:
In this example we are trying to implement a one liner which will print the given message on the console. To print the given message, we are using -e here. To run one liner on your machine you have to have Perl shell install on machine.
Code:
$ perl -e 'print "Demo to show one liner in perl";'
Output:
Conclusion
By using the one liner we can make our code in a single line, also this makes our code easy to understand and readable. One liner are very fast to execute this are inbuilt used for spacing, modification etc. We can also reverse text. Perl provide us so many features to do various things like -l, -n, -p can be used to print, new line in the operation and so on.
Recommended Articles
This is a guide to Perl one liner. Here we discuss the introduction, how perl one liner works? advantages and example respectively. You may also have a look at the following articles to learn more –