PERL NAMES,VALUES AND VARIABLES BY SANA MATEEN 6/18/2016 1
NAMES IN PERL  Perl manipulates variables which have a name.  A value is assigned to/stored in variable by assignment statement of the form name=value  Perl distinguishes between singular name and plural name. A singular name –holds single item of data– scalar value A 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
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
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
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 = $agen"; print "Name = $namen"; print "Salary = $salaryn"; 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 Scalars A scalar is most often either a number or a string. Following example demonstrates the usage of various types of numeric scalars − 6/18/2016 5
#!/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 = $integern"; print "negative = $negativen"; print "floating = $floatingn"; print "bigfloat = $bigfloatn"; print "octal = $octaln"; print "hexa = $hexan"; This will produce the following result − integer = 200 negative = -300 floating = 200.34 bigfloat = -1.2e-23 octal = 255 hexa = String Scalars Following 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 = $varn"; print "quote = $quoten"; print "double = $doublen"; print "escape = $escapen"; 6/18/2016 6
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- ‘Fridayn’  ‘Friday’--- String  ‘Fridayn’---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 7
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 8
<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  Unix ctrl - 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 9

Unit 1-perl names values and variables

  • 1.
  • 2.
    NAMES IN PERL Perl manipulates variables which have a name.  A value is assigned to/stored in variable by assignment statement of the form name=value  Perl distinguishes between singular name and plural name. A singular name –holds single item of data– scalar value A 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
  • 3.
    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
  • 4.
    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
  • 5.
    A scalar isa 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 = $agen"; print "Name = $namen"; print "Salary = $salaryn"; 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 Scalars A scalar is most often either a number or a string. Following example demonstrates the usage of various types of numeric scalars − 6/18/2016 5
  • 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 = $integern"; print "negative = $negativen"; print "floating = $floatingn"; print "bigfloat = $bigfloatn"; print "octal = $octaln"; print "hexa = $hexan"; This will produce the following result − integer = 200 negative = -300 floating = 200.34 bigfloat = -1.2e-23 octal = 255 hexa = String Scalars Following 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 = $varn"; print "quote = $quoten"; print "double = $doublen"; print "escape = $escapen"; 6/18/2016 6
  • 7.
    STRING CONSTANTS/LITERALS  Stringconstant 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- ‘Fridayn’  ‘Friday’--- String  ‘Fridayn’---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 7
  • 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 8
  • 9.
    <STDIN>  <STDIN>is usedfor 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  Unix ctrl - 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 9