Docstoc

Perl Basics

Document Sample
Perl Basics Powered By Docstoc
					gcrindia@gmail.com

PERL
Chapter-1 (Introduction)
Scripting Languages Vs Programming Languages Scripting Language 1) It is an Interpreter based Language 2) Interpreter converts high level instructions into machine language line by line 3) It doesn‟t create executable file. 4) No need to compile the program 5) It takes less code 6) It greatly reduces development time 7) It reduces maintenance of cost Programming Language 1) It is a compiler based Language. 2) Compiler converts the whole program in single short into machine language. 3) It Creates .exe file. Need to compile the program 4) It takes numerous lines of code 5) It increases development time 7) It Increases maintenance of cost

PERL-Practical Extraction and Reporting Language o Perl was originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987, as a general-purpose Unix scripting language to make report processing easier. Perl is a high-level, general-purpose, interpreted, dynamic Scripting language. The home of perl is UNIX Perl Inherited features from UNIX utilities (awk, sed, grep, Smalltalk, Lisp, C, C++, Pascal and UNIX shell. Perl written in C language. Perl is free software. Perl is open source code. Perl is highly portable Language (supports 76+ Operating systems, Scripts developed in one operating system allows to run on other operating system with/without modifications.) Perl supports oops concepts known as object oriented perl. Perl supports database connectivity. Perl has given CPAN (Comprehensive Perl Archive Network) modules.website is www.cpan.org Perl was originally developed for to do manipulations in the text files later it is used in wide range of the following areas    System Administration Database Administration Network Programming

o o o o o o o

o o o o

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

1

gcrindia@gmail.com Web Development Software Testing Hardware Testing Telecom Vlsi Baoinformatics and so on

      o o o o o o

It is built-in with UNIX and Its flavers like Linux, Solaris etc… Perl is a case sensitive Language. Each and every statement should end with (;) semicolon. Perl is an interpreter based Language. Extension of perl program is .pl or .plx Perl supports two types of comments.   Single line comment (#) Multi line comment Pod (perl old document) ------

-----Cut Perl program compilation Process:

Writing a program to display welcome message #! C:\perl\bin\perl print “welcome to Perl \n”; print “This is my first program”; Save with “first.pl” Running perl Program in Windows C:\perl first.pl #! It is shebang statement, used for to invoke perl interpreter path. Running perl Program in UNIX/Linux G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

2

gcrindia@gmail.com $ which perl # it displays location of perl usr/bin/perl perl –v # version of perl $ man perl # help document $ vi first.pl #! usr/bin/perl print “welcome to Perl \n”; print “This is my first program”; $ perl first.pl or $ chmod 755 first.pl $./first.pl

Chapter-2 (Variables)
Variables o o o o o o It is a Data name or memory location name. It is used for to store data value. Value can change during execution of the program It is a temporary storage location. Perl allows implicit variable declaration. Every variable occupies memory at runtime. Scalar Variables Array Variables or List Variables Hash Variables or Associate Array Variables It holds one value. The value may be Integer or Float or String. The Variable should begin with $ symbol.

Variables are classified into 3 types o o o

1) Scalar Variables o o o

a=10; - wrong $a=10; - right 2) Array Variables o o It is a group of scalar values It should begin with @ symbol.

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

3

gcrindia@gmail.com 3) Hash Array Variables o o It is a group of key pair values. It should begin with % symbol.

1) Scalar Variables $a=10; # Integer $b=1.5; # Float $c=”Gcreddy” #String $d=”perl is a scripting language” # String $e=”100” # String $f=‟Perl‟ # String Strings are classified into 3 types 1) Double quoted strings (“ “) or qq with delimiter of any one || or [] or {} or <> 2) Single quoted strings („ „) or q with delimiter of any one || or [] or {} or <> 3) Back tick / quoted strings (` `) or qx with delimiter of any one || or [] or {} or <> a) $str=”perl”; $x= “I like $str”; print $x; Output: I like Perl b) $x= „I like $str‟; print $x Output: I like $str c) $x= “I like perl \n $str” print $x; output: I like Perl d) $x= „I like \n $str‟; print $x; output: I like $x; e) $str= “I said “Don‟t write to disk””; # wrong $str= “I said |“Don‟t write to disk|””; # right $str= „I said “Don‟t write to disk”‟; # wrong $str= „I said “Don|‟t write to disk”‟; # right $str= qq| I said “Don|‟t write to disk”|; # right $str= q|I said “Don‟t write to disk”|; # right f) $x= “Unix/Linux”; # right G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

4

gcrindia@gmail.com $x= qq|Unix/Linux|; # wrong $x= qq|Unix\/Linux|; # right $x= qq{Unix/Linux}; # wrong note: qq for variable substitute q for as it is qx for OS command # $x= `dir`; `md xyz`; $x= qx|dir|; print $x; perl –c pl.pl # compilation ok Writing output: Perl p1.pl>a1 # over write Perl p1.pl>>a2 # add Perl p1.pl> D:\a3 # path Standard Input / Output Handlers print () – it used for to write data to the screen print(“Hello”); or print “Hello”; or print STDOUT “Hello”; I/O handlers 1) STDIN 2) STDOUT 3) STDERR STDIN – It used for to accept input from user Ex: write a program accepting name and display? Print “What is your name”; $name=<STDIN>; or <> # Diamond operator print “Hello $name, Good morning”; chmod ($name); # It is a pre-defined function, it deletes the given string lost line character if it is new line. Ex2: write a program accept 2 integer values and find sum? Print “Enter a number 1: “; Chomp ($a= <STDIN>);

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

5

gcrindia@gmail.com Print “Enter a number 2: “; Chomp ($b= <STDIN>); $c= $a + $b print “\n $a + $b= $c”;

Chapter-3 (Operators)
1) Arithmetic Operators: +, -, *, /, %, ** (right to left) a) $a=10; $b=20; print $a + $b; output: 30 b) $a=10; $b=25abc; print $a + $b; output: 35 a) $a=10; $b=”abc20; print $a + $b; output: 10 d) $a=10; $b=20abc34; print $a + $b; output: 30 e) $a=”perl”; $b=”gcreddy”; print $a + $b; output: 0 f) $a=10; $b=”25abc”;

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

6

gcrindia@gmail.com print $a + $b; output: 35 print $a .$b; output: 1025abc g) $a=2; $b=3; print $a ** $b; output: 8 h) $a=2; $b=3; $c=2; print $a ** $b ** $c; output: 512 i) $a=2; $b=3; $c=2; print ($a ** $b) ** $c; output: 64 2) Relational Operators: i) ii) Numeric Comparison Operators <, >, <=, >=, ==, !=, <=> String Comparison Operators lt, gt, le, ge, eq, ne, cmp a) $a=100; $b=20; $a>$b; output: true $a gt $b; output: false (ansii nos) b) $x= “tecno”; $y= “harika”; G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

7

gcrindia@gmail.com $x gt $y; output: true $x > $y; output: false (0,0) c) $a=100; $b=20; $c= $a<=>$b if a>b output is 1 a<b output is -1 a==b output is 0 d) $x= “tecno”; $y= “harika”; $k=$x cmp $y if x>y output is 1 x<y output is -1 x==y output is 0 3) Logical Operators: && (or) and || (or) or ! (or) not 4) Assignment Operators: (=) $a=10; $b=20; $c=30; or ($a, $b, $c) = (10,20,30); 5) String Multiplication Operators: (x) Ex: $str=”perl”; $k= $str x 5; G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

8

gcrindia@gmail.com print $k; output: perl perl perl perl perl ex2: print “-“; x 50 output: ---------------------------------------------ex3: print “_“; x 50 output: _________________________________________________ 6) Range Operators: (..) 1..10 # 12345678910 a..z #abcdefghijklmnopqrstuvwxyz h..t # hijklmnopqrst -10..1 #-10-9-8-7-6-5-4-3-2-101 10..1 # wrong -1..-10 # wrong z..a # wrong 7) String concatenation Operators: (.) It is used for to join two or more strings $str= “Load”; $str=$str. “ing”; 8) Short hand Assignment Operators: +=, -=, *=, /=, %=, .=, x= $a-10; $a= $a+5; (or) $a+=5; 9) Conditional Operators or Ternary operators: (?:) expression 1 ? expression 2 : expression 3 $a=100; $b=99; G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

9

gcrindia@gmail.com $a > $b ? print “$a is big” : print “$b is big”; 10) Incremental Operator (++) $a=10; $a=$a+1; or $a+=1; or $a++; 11) Detrimental Operators (--) $a=$a-1; or $a-=1 or $a--; -------------------****----------------****------------------

Chapter-4 (Control flow statements)
a) Conditional Statements:
1) Simple if condition if (condition) { Statements ---------------------} Statements ---------------------2) Simple unless unless (condition) { Statements ---------------------} Statements ----------------------

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

10

gcrindia@gmail.com 3) If….else if (condition) { Statements ---------------------} else { Statements ---------------------} Statements ---------------------4) unless….else unless (condition) { Statements ---------------------} else { Statements ---------------------} Statements G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

11

gcrindia@gmail.com ---------------------5) if….elseif…else if (condition) { Statements ---------------------} else if (condition) { Statements ---------------------} else if (condition) { Statements ---------------------} else { Statements ---------------------} Statements --------------G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

12

gcrindia@gmail.com -------6) Single line if statement statement if (condition); Statements --------------7) Single line unless statement statement unless (condition); Statements ---------------

b) Loop Statements:
1) While Loop while (condition) { Statements ---------------------} Statements ---------------------2) until Loop until (condition) { Statements ---------------------} Statements ---------------

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

13

gcrindia@gmail.com -------3) Do while Loop do { Statements ---------------------} while (condition) { Statements ---------------------} 4) Do until Loop do { Statements ---------------------} until (condition) { Statements ---------------------} 5) For Loop for (initiation; condition; increment/decrement) { Statements ---------------

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

14

gcrindia@gmail.com -------} Statements ---------------------6) Foreach Loop foreach variable (list of variables) { Statements ---------------------} Statements ---------------------7) Last keyword: It is used for to terminate the loop; it is same as break in „C‟ Language. While (condition) { Statements --------------last; --------------} Statements ---------------------8) Next keyword: It is a keyword, used for to place control at beginning of the loop. It is same as continue in „C‟ language.

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

15

gcrindia@gmail.com

While (condition) { Statements --------------next; --------------} Statements ---------------------Examples: 1) Write a program accepting a number and check given number is 3 digit number or not? Print “Enter a number: “; Chomp ($n= <STDIN>); If ($n >=100 && $a <=999) { print “$n is a 3 digit number”; } else { print $n is not a 3 digit number”; 2) Write a program accepting user name and password and check for given user name & password are valid or not? Print “\n Enter user name”; Chomp ($uname= <STDIN>); Print “\n Enter password”; Chomp ($pwd= <STDIN>); If ($uname eq “tecno” && $pwd eq “soft” { print “\n welcome to tecnosoft”; } else { print “\n invalid user name or password”; }

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

16

gcrindia@gmail.com 3) Write a program to print numbers 1 to 10? $num=1; while ($num ,=10) { print “$num \n”; $num++ } (or) for ($num=1; $num <= 10; $num++) { print “$num \n”; } (or) foreach $num(1..10) { print “$num \n”; } (or) foreach (1..10) { print “$_ \n”; } (or) foreach (1..10) { print ; } Note: $_ is Perl special and default variable. If we don‟t declare any variable for reading data then Perl will store the value in default variable.

G.C.Reddy, Test Consultant (Mobile: 91-9247837478)

17


				
DOCUMENT INFO
Shared By:
Stats:
views:677
posted:1/8/2010
language:English
pages:17
Description: This Document Describes Perl Script Basics