unit 1-perl names values and variables

9
PERL NAMES,VALUES AND VARIABLES BY SANA MATEEN 6 / 1 8 / 2 0 1 6 1

Upload: sana-mateen

Post on 08-Apr-2017

17 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Unit 1-perl names values and variables

1

PERL NAMES,VALUES AND VARIABLESBYSANA MATEEN

6/18/2016

Page 2: Unit 1-perl names values and variables

NAMES IN PERL Perl manipulates variables which have a name. A value is assigned to/stored in variable by assignment statement of the

formname=value

Perl distinguishes between singular name and plural name.A singular name –holds single item of data– scalar valueA plural name for variable – hold collection of data items —an array or hash

Starting special character of variable denotes the kind of thing that name stands for

$ ---- Scalar data@ ----- Array% ----- Hash& ----- Sub routine

6/18/2016

2

Page 3: Unit 1-perl names values and variables

NAMES IN PERL... Valid characters are letters,digits,underscores. First character after special character can be a letter or underscore. Names may also have non-alphanumeric character after special character.

$$,$? (system reserved names in Perl ) Each kind of data has separate namespace. Special character determine the context in which the name is being used. In C language a new variable is declared as

int i=1;float data[9];

Scope of variable depends on the part of program in which the variable is visible and available for use.

Global scope and local scope. Variable declaration in perl –

$a=5;my $a=10;

A variable comes into existence when declared or first used with special value denoted by undef

undef $x;

6/18/2016

3

Page 4: Unit 1-perl names values and variables

NAMES IN PERL...

use strict ‘var’; (or) use strict; It tells perl to insist on declaration by placing the line. At the start of script variables are declared using my $x,$y;

#!/usr/bin/perl @ages = (25, 30, 40); @names = ("John Paul", "Lisa", "Kumar"); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] =

$names[2]\n";

6/18/2016

4

Page 5: Unit 1-perl names values and variables

5

A scalar is a single unit of data. Perl recognizes two kinds of scalar data , a String and Numbers . There’s no difference between integers and real numbers both are same.Here is a simple example of using scalar variables −

#!/usr/bin/perl $age = 25; # An integer assignment $name = "John Paul"; # A string $salary = 1445.50; # A floating point print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n";

This will produce the following result −Age = 25 Name = John Paul Salary = 1445.5

Strings are stored as sequence of bytes of unlimited length . Perl is dynamically typed language (System keeps track of whether a variable contains a numeric value or string value).Depending on the context strings are converted to int.Eg:If int/num occurs in String context, operand for string operator , perl will convert it to string

Numeric ScalarsA scalar is most often either a number or a string. Following example demonstrates the usage of various types of numeric scalars −

6/18/2016

Page 6: Unit 1-perl names values and variables

6

#!/usr/bin/perl $integer = 200; $negative = -300; $floating = 200.340; $bigfloat = -1.2E-23; # 377 octal, same as 255 decimal $octal = 0377; # FF hex, also 255 decimal $hexa = 0xff; print "integer = $integer\n"; print "negative = $negative\n"; print "floating = $floating\n"; print "bigfloat = $bigfloat\n"; print "octal = $octal\n"; print "hexa = $hexa\n";

This will produce the following result −integer = 200 negative = -300 floating = 200.34 bigfloat = -1.2e-23 octal = 255 hexa = 255

String ScalarsFollowing example demonstrates the usage of various types of string scalars. Notice the difference between single quoted strings and double quoted strings −#!/usr/bin/perl $var = "This is string scalar!"; $quote = 'I m inside single quote - $var'; $double = "This is inside single quote - $var"; $escape = "This example of escape -\tHello, World!"; print "var = $var\n"; print "quote = $quote\n"; print "double = $double\n"; print "escape = $escape\n";

6/18/2016

Page 7: Unit 1-perl names values and variables

7

STRING CONSTANTS/LITERALS String constant and literals can be enclosed in single or double quotes. The string is terminated by first next occurrence of quote which started it , so single

quoted strings can include double quotes and vice versa. Single quoted strings are treated as it is-

‘Friday\n’ ‘Friday’--- String ‘Friday\n’---String with seven characters including last character which is a new

line. \n-newline,\t-tab,\U-uppercase There is more than one way to choose your own quote 1.quote — q 2.double quote– qq q /any string/ or q(any string) and qq(any string), qq /any string/

6/18/2016

Page 8: Unit 1-perl names values and variables

8

VARIABLES AND ASSIGNMENT Perl uses – ‘=‘ as the assignment operator. It returns a value. This permits

statement like $b=4+($a=3); $a=“Burger”; $b=“Sandwich $a” //$b would give “Sandwich Burger” $c=“turkey $a”; Scalar variable names start with--$ $a=“java”; $b=“${a} script”;//value is javascript

6/18/2016

Page 9: Unit 1-perl names values and variables

9

<STDIN> <STDIN>is used for acquiring input from keyboard.If no input is queued

perl will wait until a line is typed and the return key pressed. End-of-file

ctrl - D Unixctrl - Z DOS

They cause the return to be undefined, it evaluates to “ “ . The empty string is treated as false in boolean context.

while(<STDIN>){.....}To process all statements until the end of file is reached.While(defined <STDIN>){...}

6/18/2016