PERL/C2/Variables-in-Perl/English

From Script | Spoken-Tutorial
Revision as of 18:44, 18 March 2013 by AmolBrahmankar (Talk | contribs)

Jump to: navigation, search

Title of script: Variables in Perl

Author: Amol

Keywords: Variables in Perl, Video Tutorial, Scalars in Perl


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

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

Slide Variables in Perl:

Variables are used for storing values, like text strings, numbers or arrays.

Once a variable is declared, it can be used over and over again in the script.

Slide Scalar represents a single value and can store scalars only.

Scalar variables are declared using $ (dollar) symbol.

Slide Let us look at Variable Declaration:

A variable can be declared as follows:

dollar priority semicolon

$priority;


Variable names in Perl can have several formats. Variables must begin with a letter or underscore

Slide And may contains letters, digits, underscores or a combination of above 3.
Slide Variables declared with CAPITAL letters have special meaning in Perl.

So avoid declaring variables using Capital letters.

Switch to the Terminal and type

gedit variables.pl &

and press Enter.

Now Open the Terminal and type

gedit variables.pl &


The ampersand will unlock the command prompt on the terminal)

Now press Enter.

Point to the filename variables.pl in the Titlebar of gedit. This will open the variables.pl file in gedit text editor.

dot pl is the default extension of a Perl file.

Type the following

$priority;

and press Enter.

Type the following in the file;

dollar priority semicolon

$priority;

and press Enter.

Highlight $priority; with the cursor So we have declared the variable priority.

You do not need to declare a variable before using it;

you can just use it into your code.

Type

$priority = 1;


Now let us assign a numerical value to the variable priority


for this Type

dollar priority space equal to space one semicolon

$priority = 1;

and press Enter.

Type

print “Value of variable is: $priority”;

Next, type

print space double quote Value of variable is: dollar priority slash n double quote complete semicolon

slash n is the new line character

Save this file as variables.pl at any location of your choice.

I am storing it in my home directory /home/amol.

Now Save this file as variables.pl at any location.

In my case, it will get saved in /home/amol directory. Now save this file

Switch to the Terminal and type

chmod 755 variables.pl

explain the need of above line and then press Enter.

Now, let us change the permissions of variables.pl Perl file which we just created.

To do so, on the Terminal type,

chmod 755 variables.pl

This will provide read, write & execute rights to the file.

Switch to the Terminal and type

perl -c variables.pl

but don't press Enter yet.

To compile this Perl script, on the Terminal.

Type the following command;

perl hyphen c variables dot pl

Highlight -c using the cursor. Hyphen c switch compiles the Perl script for any compilation/syntax error.
Now press Enter Now press enter

This tells us that there is no syntax error in our script

Highlight variables.pl syntax OK


Type

perl variables.pl

and press Enter.

Now Let's execute the Perl script by typing

perl variables dot pl

and press Enter.

Point with the cursor to

Value of variable is: 1

The output displayed is as highlighted
Switch back to the Text editor window. We can also assign a string value to the variable we declared.

Switch back to the Text editor window.

Highlight 1; with the cursor

Instead of 1 type

'high'

Instead of dollar priority equal to one;

type

dollar priority equal to in single quote high

Point from right of = to left. Please note that assignments are evaluated from right to left.

A scalar can hold data of any type, be it a string, a number

Switch to the Terminal and type

perl -c variables.pl

and press Enter.

Save this file and compile the script once again by typing


perl hyphen c variables dot pl

now press Enter.

Type

perl variables.pl

and press Enter.

This tells us that there is no syntax error.


Execute the script by typing

perl variables dot pl

and press Enter.

Point with the cursor to

Value of variable is: high

The output is as shown
Switch back to the Text editor window.

Go to the last line and press Enter.

Type

$priority = “String”;

Now Switch back to the Text Editor window.


You can also use scalars within a double-quoted strings as

dollar priority in double quote String


Save this file and close it

Now let us learn how to declare multiple variables.

To do so open the new file in Text Editor.

Switch to the Terminal and type

gedit multivar.pl &

and press Enter.

onTerminal type -

gedit multivar dot pl space &

and press Enter.


This will open multivar dot pl file in text editor

Type

$firstVar, $secondVar;

and press Enter.

Now will type -

dollar firstVar comma dollar secondVar semicolon

and press Enter.

Type

$firstVar = $secondVar;

and press Enter.

To copy the value of variable dollar firstVar to dollar secondVar, type -

dollar firstVar space equal to space dollar secondVar semicolon

and press Enter.

Switch to slide


All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables

Let us see how we can achieve this using Perl.

$firstVar = $secondVar =10;

Press Enter.

Switch to text editor and now Let us assign the value 10 to both these variables by typing dollar firstVar equal to dollar secondVar equal to ten semicolon.

And Press Enter.

Type

print “firstVar: $firstVar and secondVar: $secondVar”;

and press Enter.

Now To print the values, type

print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n double quote complete semicolon press Enter.

Now save this file.

Type

$addition = $firstVar + $secondVar;

Press Enter.

Now let us add the values in the two variables.

For this type

dollar addition space equal to space dollar firstVar plus space dollar secondVar semicolon

and Press Enter.

Highlight the variable addition with the cursor Notice, we haven't declared the variable addition.
Type

print “Addition: $addition”;

Once again to print the value of the addition, type

print double quote Addition is dollar addition slash n double quote complete semicolon

Save this file

Switch to the Terminal and type

perl -c variables.pl

and press Enter.

To compile this file again on terminal type

perl hyphen c multivar dot pl

there is no syntax error so

we can execute the script by typing

Type

perl multivar.pl

and press Enter.

perl multivar dot pl



Point with the cursor to the output This will produce an output as highlighted
Pause the video recording here


Type out the code for subtraction, multiplication and division in the multivar.pl file.


Resume the video recording now


Also, compile the code so that the output is displayed on the Terminal.

Switch to gedit Similarly, try subtraction, multiplication and division.


I have written the code here.

Close this file in gedit Now Let us save this file and close it.
Show the output in the terminal Now compile the file by typing

perl hyphen c multivar dot pl

There is no syntax error so we can execute the script as \

perl multivar dot pl


On execution the output will look like this

Summary This brings us to the end of this tutorial.

In this tutorial we have learnt,

To Declare and Use scalar variables in Perl



Assignment Assignment
  1. Declare a number variable.
  2. Assign the value 10 to it.
  3. Print the variable declared.
  4. Declare 2 string variables.
  5. Assign these values to them - “Namaste ” and “India”.
  6. Print those string variables one after the other.


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