How to write comments in Perl language? Comments contain a description, explain single or multiple lines of a code
A developer can better understand the code by reading the comments. These are ignored at runtime.
In Perl, You can write a comment in different following ways.
- Single-line comments
- Multi-Line comments
- Documentation commands
It is always good practice to add Comments to the below blocks or lines of code.
Perl Single line comments
Single-line or inline comments always contain a single line.
- Comment line always starts with hash(#) symbol followed by comment text.
- Comment text describes a single line of code.
- These comments can be placed at the start at the beginning of the line but also can be written in the middle or end of the line. Syntax:
# This is single line comments in Perl Code
Example
hello.perl:
# Hello World application
print "Hello World application"
Perl Multi-line comments
Multi-line comments also called block comments, can be written in multiple lines.
Here is a way we can write a multi-line comment.
Multi-line Comments always start with =
and are followed by single or multiple lines and end with =cut
= multi-line comments 1
multi-line comments 2
multi-line comments 3
=cut
You can also write multi-line comments with single-line comment syntax as given below.
= multi-line comments
//multi-line comments 2
//multi-line comments 3
=cut
This allows you to write multiple lines of code and be ignored by the compiler.
Block level comments in Perl Example:
= Hello world sample program
Basic program to print hello world to console
First code to write in Perl programming language
=cut
print "Hello World app" ;