Updated April 3, 2023
Introduction to Perl Comments
Perl comments is used to make our program user friendly to the user. Comments part is skipped by interpreter in perl language and other part which was not in comments will be executed. There are two types of comments available like single line comment and multiple line comment. Single line comment is used when we have to give comment only in single line, multiple line comment is used when we have to give comment of multiple lines at one time. Comment is very useful and important in language.
Types of Perl Comments
Given below are two types of comments with example:
- Single Line Comments
- Multiple Line Comments
Comment is necessary and useful in perl language while writing any code. If we have not written any comment in code then it is difficult to understand the code to new user. Comment are very useful and important. Comments are providing basic explanation of a code or program. After adding comments in program we can easily understand program after long time, also it is suitable to new user to understand the program or code. We can comment single line comment using a # sign. Hash sign is used to comment single line. We can comment multiple line using “= begin” and “= cut” keyword. It is used to make our program user friendly to the user, comments part is skipped by interpreter in and other part which was not in comments will be executed.
1. Single Line Comments
- Single line comment is used in perl language when we have to give comment only in single line.
- Single line comment start with a hash (#) sign. Single line comment starts and end with same line. If our statement is not end in first line then we have to mention again hash sign in second line.
- Single line comment is very useful and important.
Example:
Code:
my $emp_fname = 'AB'; # Declaration of string scalar data type
my $emp_age = 35; # Declaration of integer scalar data type
my $emp_salary = 10000.50; # Declaration of floating point number scalar data type
use warnings; # Show warning of code
if (0<1) # Execute inside section when condition is true
{
print "$emp_fname\n"; # Initialization of emp_fname variable.
print "$emp_age\n"; # Initialization of emp_age variable.
print "$emp_salary\n"; # Initialization of emp_salary variable.
} # End of code
Output:
- In above code we have used a single line comment to every line. We have observed that comment line code was not executed by interpreter. It only executes non comment line code and display the output.
- Single line comment is used when we have given comment to a single line.
2. Multiple Line Comments
- We can comment multiple line using “= begin” and “= cut” keyword in Perl language.
- Multiple line comment is very useful and important.
- Multiple line comment is used in perl language when we have to give comment in multiple line for same time we have used multiple line comment.
- Perl interpreter could not execute section in under multiple line comment. It will find “=begin” keyword after finding it will skip the execution until “= cut” keyword is not getting.
- Multiple line is more important as compared to single line comment. While using multiple line comment there is no need to define “= begin” and “= cut” keyword at every line. We need to define it only at the time of starting and ending of the comment.
Example:
Code:
my $emp_fname = 'ABC';
my $emp_age = 40;
my $emp_salary = 20000.50;
=begin
Let's declare the emp_fname as string scalar data type,
emp_age as integer scalar data type,
emp_salary as floating point number scalar data type.
=cut
use warnings;
=begin
Show warning
Which was occurred in code
=cut
if (0<1)
{
print "$emp_fname\n";
print "$emp_age\n";
print "$emp_salary\n";
}
=begin
Initialization of emp_fname variable.
Initialization of emp_age variable.# End of code
Initialization of emp_salary variable.
=cut
Output:
- In above code we have used a multiple line comment to multiple lines. We have observed that comment line code was not executed by interpreter. It only execute non comment line code and display the output.
- Multiple line comment is used when we have given comment to a multiple line. Multiple line comment is more useful as compared to single line comment.
Advantages of Perl Comments
Below are the advantages:
- Using comment is perl we explain which content we have used in program or code.
- Code description is easy using comment.
- Debugging of a code or program is easy while we are using a comment.
- While using comment in perl program we increase the code readability.
- While using comment in program we will make it user friendly. Anyone can understand and work on it.
- We can easily find the implemented code logic easily using comment.
- While using comment in perl we can easily identify the issue and solve it with in small time.
- It is useful and it will increase the readability of a code.
- It is used to increase the reusability of a code which is already implemented few days ago.
- Comments in perl is useful to enhance the comprehensibility of a program.
- Comments are very useful and important in perl to describe the contents of code.
Conclusion
Basically there are two types of comments available in perl like single line and multiple line comment. Single line comment is used when we have to give comment only to a single line, multiple line comment is used when we have to give comment to multiple lines at one time.
Recommended Articles
We hope that this EDUCBA information on “Perl Comments” was beneficial to you. You can view EDUCBA’s recommended articles for more information.