Welcome to the spoken tutorial on Comments in Perl. In this tutorial, we will learn about Comments in Perl I am using Ubuntu Linux12.04 operating system and Perl 5.14.2 that is, Perl revision 5 version 14 and subversion 2 I will also be using the gedit Text Editor. You can use any text editor of your choice. You should have Basic knowledge of Compiling, Executing and Variables in Perl If not, please go through the relevant spoken tutorials on the spoken tutorial website Commenting a piece of code in Perl can be done in two ways: Single Line Multi Line This type of comment is used when user wants to comment a single line of code or to add one liner text to explain the functionality of a piece of code This type of comment starts with the symbol # (hash) . Here is a demo. Let us open a new file in the Text Editor. Open the Terminal and type – gedit comments dot pl space & Once again, reminding you that the ampersand is used to free the command prompt in the terminal and press enter. Now type the following commands. hash Declaring count variable ‘press Enter dollar count space equal to space 1 semicolon press enter print space double quotes Count is dollar count slash n close double quote semicolon space hash prints Count is 1 Now Save this file by pressing ctlr S and execute the Perl script. Switch to the Terminal, and type perl hyphen c comments dot pl and press Enter. This tells us that there is no syntax error Now type perl comments dot pl and press Enter. It will show the following output – Count is 1 Let us switch back to gedit. In gedit, go to the first line and press enter. Go back to the first line and type the following command. Hash exclamation mark slash usr slash bin slash perl This line in Perl is called as a shebang line and is the first line in a Perl program. It tells where to find the Perl Interpreter. Note: Though this line starts with hash symbol, it will not be considered as a single line comment by Perl. Now let us look at multiline comments Multi Line This type of comment is used when user wants to comment a piece of code or to add description/usage of piece of code This type of comment starts with the symbol equal to head and ends with equal to cut Lets switch back to gedit and type the following in the comments dot pl file at the end of file type equal to head, press Enter print space double quote count variable is used for counting purpose close double quote press enter. equal to cut Save the file, close it and execute the Perl script. On the Terminal, type perl hyphen c comments dot pl and press Enter. No syntax error so let us execute it perl comments dot pl It will show the same output as before. Count is 1 It does not print the sentence “count variable is used for counting purpose” This is because we commented the portion using equal to head and equal to cut You can either use =head=cut or=begin=end. These are not the special keywords used by Perl. Please note there should not be any leading or trailing space(s) before=to sign and after the head, cut, begin or end word. Open the Terminal once again. And Type – gedit commentsExample dot pl space & and press Enter. Type the following commands as shown on the screen. Here I am declaring two variables firstNum and secondNum and I am assigning some values to them. Then I have commented this portion here. Now I added these two numbers and assign the value to a third variable named addition. Next I want to print the value using print command. Save the file and execute the Perl script on the Terminal. On terminal type perl hyphen c commentsExample dot pl, press Enter. There is no syntax error So execute the script by typing perl commentsExample dot pl press enter It will show the following output. Addition is 30 This brings us to the end of this tutorial. Here we learnt, To Add Comments in Perl Write a perl script to find square of a number. Explain the functionality of the code written using: Single Line Comment & Mutli Line Comment. Watch the video available at the following link It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact at spoken hyphen tutorial dot org Spoken Tutorial Project is a part of the Talk to a Teacher project It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at the following link. Hope you enjoyed this Perl tutorial. This is Amol Brahmankar signing off.
Thanks for joining.