PERL/C2/Comments-in-Perl/English

From Script | Spoken-Tutorial
Revision as of 11:42, 21 March 2013 by AmolBrahmankar (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Comments in Perl

Author: Amol

Keywords: Comments in Perl, Video Tutorials, Scalars in Perl


Visual Cue
Narration
Slide Welcome to the spoken tutorial on Comments in Perl.
Slide:Learning Objectives In this tutorial, we will learn about

Comments in Perl

Slide:System Requirements 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

Slide Commenting a piece of code in Perl can be done in two ways:

a. Single Line

b. Multi Line

Slide Single 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.

Switch to the Terminal and type

gedit singlemulti.pl &

and press Enter.

Open the Terminal and type -

gedit comments dot pl &



Highlight & Once again, reminding you that the ampersand is used to free the command prompt in the terminal. And press enter
Type

# Declaring count variable

$count = 1;

print “Count is $count \n”; # prints Count is 1

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 double quote complete semicolon space hash prints Count is 1

Save the file. Now Save this file by pressing ctlr s file and execute the Perl script.
Switch to the Terminal and type

perl -c singlemulti.pl

and press Enter

Switch to the Terminal, and type

perl hyphen c comments dot pl

and press Enter.

This tells us that there is no syntax error



Then type

perl singlemulti.pl

and press Enter

Now type

perl comments dot pl

and press Enter.

Point to the output It will show the following output -

Count is 1

Go to gedit and type

#!/usr/bin/perl


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 code.

Hash exclamation mark slash usr slash bin slash perl



Slide 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.

Slide 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

Slide 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

Type

=head

print “ count variable will be used for counting purpose”

=cut

above

$count = 1;

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 will be used for counting purpose double quote complete

press enter

equal to cut

Save the file and close it. Save the file, close it and execute the Perl script.
Switch to the Terminal and type

perl -c singlemulti.pl

and press Enter.

On the Terminal, type

perl hyphen c comments dot pl

and press Enter.

No syntax error

Then type

perl singlemulti.pl

and press Enter

so let us execute it

perl comments dot pl

and press Enter.

Point to the output It will show the same output as before.

Count is 1

Switch back to gedit

Point to the sentence

“count variable will be used for counting purpose”

It does not print the sentence “count variable will be used for counting purpose”
Point to =head and =cut This is because we commented the portion using equal to head and equal to cut
Slide 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.
Switch to the Terminal Open the Terminal once again.
Type

gedit comments.pl &

and press Enter.

And Type -

gedit commentsExample dot pl &

and press Enter.

Type

# Here I am going to explain commenting in Perl

# Following is the scalar variable declaration

$firstNum = 10;

$secondNum = 20;


=head

following piece of code adds 2 numbers

puts the result into a 3rd variable

and prints this 3rd variable

=cut

$addition = $firstNum + $secondNum;

print “Addition is $addition”;

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 switch to the Terminal.

Type

perl -c comments.pl

perl comments.pl

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

Point to the output It will show the following output.

Addition is 30

Summary This brings us to the end of this tutorial.

Here we learnt,

To Add Comments in Perl

Assignment Assignment
  1. Write a perl script to find square of a number.
  2. Explain the functionality of the code written using:
  • Single Line Comment.
  • Mutli Line Comment.


About the Project 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

Spoken Tutorial Workshops 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

Acknowledgment 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.

Contributors and Content Editors

AmolBrahmankar, Nancyvarkey, Sneha