PERL : Practical Extraction Reporting Language - PowerPoint PPT Presentation

1 / 169
About This Presentation
Title:

PERL : Practical Extraction Reporting Language

Description:

PERL : Practical Extraction Reporting Language Neha Barve Lecturer Bioinformatics Devi Ahilya University ifelsif #!/strawberry/perl/bin/perl -w use strict; print – PowerPoint PPT presentation

Number of Views:449
Avg rating:3.0/5.0
Slides: 170
Provided by: NehaB
Category:

less

Transcript and Presenter's Notes

Title: PERL : Practical Extraction Reporting Language


1
PERL Practical Extraction Reporting Language
  • Neha Barve
  • Lecturer
  • Bioinformatics
  • Devi Ahilya University

2
  • Perl stands for Practical Extraction and Report
    Language.
  • Perl is known as a high-level, interpreted,
    general-purpose and dynamic programming language.
  • Perl was invented  by Larry Wall, a linguist
    working as a systems administrator at NASA in
    1987.
  • Perl run well on UNIX and Windows systems. If you
    develop Perl for UNIX system, it can be portable
    to Windows system as well.

3
What is programming?
  • Telling a computer what you want it to do.
  • The only thing Is to give instructions in a way,
    the computer can understand.
  • Usually use a language that a computer can
    understand.

4
  • Difference in Human learning and machine
    learning?
  • The most important thing is that you never going
    to express a task to computer if you cant express
    it to urself.

5
Source code/Code
  • The instruction that we give to the computer are
    collectively called as Source Code.

6
Difference between compiler aNd interpreter
7
Perl compiler or Interpreter
  • Compiler convert source code into bytecode
    (machine code) and than interpreter (denoted by
    p) convert it into real code.
  • Hence PERL is not a strictly compiled language or
    Interpreted one.

8
Is PERL a Scritping language
  • Yes it is
  • Some people say it scripting language that is
    interpreted language.
  • Perl run well on UNIX and Windows systems. If you
    develop Perl for UNIX system, it can be portable
    to Windows system as well.

9
Some key features of PERL
  • Keywords There are certain instructions that
    PERL recognizes and understand.
  • Statement and statement blocks
  • The statement in PERL ends with .
  • Blocks are used to divide coding into several
    parts to separate each statement from
    surroundings.
  • This shows that how you are passing argument to
    certain functions.

10
  • ASCII and UNICODE
  • Escape sequences Escape helps to introduce the
    characters which can be types.
  • White spaces
  • name we give to Tabs, New lines and Spaces.

11
Writing your first program
  • ! C/Perl/usr/bin/perl
  • ! C/perl/bin/perl
  • ! C/strawberry/Perl/usr/bin/perl
  • ! usr/bin/perl
  • Print hello world
  • The first line is called shebang line (use either
    of one.)
  • Which gives path to Perl compiler.

12
Data types in PERL
  • Scalars
  • Arrays (lists)
  • Associative arrays (hashes)

13
Scalars
  • Scalar variables are used to store single values
    - for example, a number or a string. They are
    preceded by the dollar sign ().
  • sigil A symbol. In Perl a sigil refers to the
    symbol in front of a variable.
  • A number can be integer or float.
  • num10
  • num10.5
  • numhello all how r u?

14
autovivication
  • !usr/bin/perl -w
  • use strict
  • use warnings
  • my diameter4
  • my circumference
  • my pie
  • print "hello world"
  • print ( circumferencediameterpie)
  • Something which is not defined and perl consider
    it as zero

15
Print function
  • Statement is ended by using .
  • There are more than one ways to do a thing.
  • How to write statement in PERL
  • !perl/bin/perl
  • Print hello world
  • Print q/hello world/
  • Print q/hello world/
  • The output will be
  • hello world
  • hello world
  • hello world

16
  • stringq(hi all)
  • string1qhi all
  • string2qlthi allgt
  • string3qhi all
  • string3qhi all
  • string3qhi all
  • print "string\n"
  • print "string1\n"
  • print "string2\n"
  • print "string3\n"
  • The output will be
  • hi all
  • hi all
  • hi all
  • hi all

Alternative delimiters
17
  • Print (hello, all, how), are, you
  • The output would be
  • Helloallhow (without space)
  • Some important points
  • U can use print or printf function
  • Using Escape characters (text formatting)
  • New line
  • Tb
  • backspace

18
Here documents
  • End of File

19
Single quote vs double quote (for windows)
  • There is no processing is done under single
    quoted string.
  • Single quote treat every argument as plain text.
  • Double quoted string will have its escape
    (backslash)sequences processed eg scalars
    interpolation.
  • Backslash are used to turn off special effect of
    any character. (overwhacking)

20
Few examples
  • Print C\\win\\bun\\perl\\
  • Print C\\win\\bun\\perl\\
  • Now what the heck is that?

21
Number system
  • We can express numbers as binary, hexadecimal,
    and octal.
  • Hex are denoted by 0X
  • Octal are denoted by 0
  • Binary are denoted by 0b

22
  • Some examples are
  • Print 255
  • Print 0378
  • Print 0b11111111
  • Print 0xFF
  • Question what are Barewords (series of
    characters that perl doesnt recognize)

Related errors Illegal octal digit Illegal
binary digit
23
Converting numbers (use of printf or sprintf)
  • Finding a octal number
  • num42
  • printf("o", num) (output 52)
  • Finding hexadecimal value (small and capital)
  • Printf (x, num)
  • Converting octal to decimal
  • Print oct(num)
  • Converting hexadecimal to decimal
  • Print hex(num)

24
  • Converting decimal to binary
  • Printf(b, 10) (output 1010)
  • Converting binary to decimal
  • iosprintf ("b",10)
  • hhoct("0b". io)
  • print hh

25
ltSTDINgt INPUT function
  • print "gimme some number"
  • oneltSTDINgt
  • chomp one
  • print oct(one)

26
  • concatenation
  • String concatenation uses the period character
    ".
  • my fullname 'mud' . "bath"
  • Print fullname

27
CGI-PERL
  • Neha Barve
  • Lecturer Bioinformatics
  • DAVV

28
Introduction to CGI
  • CGI stands for Common Gateway Interface.
  • CGI is a standard programming interface to Web
    servers that gives us a way to make our sites
    dynamic and interactive.
  • CGI is not a programming language. It is just a
    set of standards (protocols.)
  • CGI can be implemented in an interpreted language
    such as PERL or in a compiled language such as C.
  • CGI is one method by which a web server can
    obtain data from (or send data to) databases,
    documents, and other programs, and present that
    data to viewers via the web. More simply, a CGI
    is a program intended to be run on the web.

29
Introduction to CGI(continued)
  • CGI programs work as follows
  • STEP 1 (On the client side) Get Information from
    the user (using HTML forms, SSI, Java Applet,
    ,etc).
  • STEP 2 (On the server side) Process the data,
    connect to DATABASE, search for PATTERNS, ,etc.
  • STEP 3 (On the server side) Send the result of
    computation back to the client.

30
Static Pages
Request file
Retrieve file
Send file
31
Dynamic Pages
Request service
Do Computation Generate HTML page with
results of computation
Return dynamically generated HTML file
32
CGI Web Application
Request service
Run CGI program print result
HEADERS BODY
33
What do u need?
  • A text editor
  • A web server
  • Perl interpreter

34
Basics of CGI program
  • A CGI is simply a program that is called by the
    webserver, in response to some action by a web
    visitor (user)
  • if you're writing a CGI that's going to generate
    an HTML page, you must include this statement
    somewhere in the program before you print out
    anything else.
  • print "Content-type text/html\n\n"

35
Imp note
  • This is a content-type header that tells the
    receiving web browser what sort of data it is
    about to receive in this case, an HTML
    document. If we forget to include it, or if you
    print something else before printing this header,
    you'll get an "Internal Server Error" when you
    try to access the CGI program

36
Our first CGI program
  • !/usr/bin/perl -wT
  • print "Content-type text/html\n\n"
  • print "Hello, world!\n"
  • Save this file to ur CGI-BIN folder.
  • Run the web server, and run the file by typing
    the URL.
  • Eg http//localhost/cgi-bin/first.cgi

37
  • What happened in above program when u run it.

38
Now write ur second program
  • !/usr/bin/perl -wT
  • print "Content-type text/html\n\n"
  • print "lthtmlgtltheadgtlttitlegtHelloWorldlt/titlegtlt/head
    gt\n"
  • print "ltbodygt\n"
  • print "lth2gtHello, world!lt/h2gt\n"
  • print "lt/bodygtlt/htmlgt\n"
  • (This program shows you tht how u combine HTML,
    PERL and CGI)

39
Lets try our third CGI program
  • !/usr/bin/perl -w
  • print "Content-type text/html\n\n"
  • print ltltEndOfHTML
  • lthtmlgtltheadgtlttitlegtTest Pagelt/titlegtlt/headgt
    ltbodygt lth2gtHello, world!lt/h2gt lt/bodygtlt/htmlgt
  • EndOfHTML

40
CGI-PERL module
  • These are collections of pre-written code that
    can be used to do all kinds of tasks.
  • You can save yourself the time and trouble of
    reinventing the wheel by using these modules.
  • standard library modules
  • Some modules are included as part of the Perl
    distribution these are called standard library
    modules and don't have to be installed. If you
    have Perl, you already have the standard library
    modules.

41
Comprehensive Perl Archive Network (CPAN)
  • These are modules available that are not part of
    the standard library.
  • The CGI.pm Module The CGI.pm module is part of
    the standard library, and has been since Perl
    version 5.004.

42
How to include modules in CGI program
  • First include the module via the use command.
  • This goes after the
  • !/usr/bin/perl line and before any other code
  • The .pm is implied in the use statement
  • The qw(standard) part of this line indicates
    that we're importing the "standard" set of
    functions from CGI.pm.

use CGI qw(standard)
43
CGI functions
  • A function is a piece of code that performs a
    specific task it may also be called a subroutine
    or a method.
  • Functions may accept optional arguments (also
    called parameters), which are values (strings,
    numbers, and other variables) passed into the
    function for it to use

44
  • The CGI.pm module has many functions for now
    we'll start by using these three
  • header
  • start_html
  • end_html

45
The header function
  • The header function prints out the "Content-type"
    header. With no arguments, the type is assumed to
    be "text/html".
  • E.g.
  • ! C/perl/bin/perl
  • Use CGI qw(standard)
  • Print header

46
The start_html function
  • start_html prints out the lthtmlgt, ltheadgt, lttitlegt
    and ltbodygt tags. It also accepts optional
    arguments. If you call start_html with only a
    single string argument, it's assumed to be the
    page title.
  • ! C/perl/bin/perl
  • Use CGI qw(standard)
  • Print header
  • Print start_html(hello world)

47
The output will be (in CMD)
  • lthtmlgt ltheadgt lttitlegtHello Worldlt/titlegt ltheadgt
    ltbodygt
  • Giving arguments in Start_html function
  • ! C/perl/bin/perl
  • Use CGI qw(standard)
  • Print header
  • Print start_html(-titlegt page, -bgcolorgt
    cccffg, -textgt ggghhh backgroundgt
    xyz.jpg)

48
The end_html function
  • The end_html function prints out the closing HTML
    tags
  • lt/bodygt lt/htmlgt

49
A final program
  • !/usr/bin/perl -w
  • use CGI qw(standard)
  • print header
  • print start_html("Hello World")
  • print "lth2gtHello, world!lt/h2gt\n"
  • print end_html
  • (u can also include all line in one print
    function)

50
USING SCALARS IN CGI-PERL
  • !perl/usr/bin/perl
  • Use CGI qw(standard)
  • Use CGIcarp qw(warningsToBrowser
    fatalsToBrowser)
  • Use strict
  • my emailxyz_at_gmail.com
  • my domainwww.xyz.com
  • Print start_html(scakars)
  • Print ltltendofhtml
  • lth2gtthese are scalarslt/h2gt
  • ltpgt My e-mail address is email, and my web url
    is lta href"url"gturllt/agt. lt/pgt
  • Endofhtml
  • Print end_html

51
  • Use CGIcarp qw(warningsToBrowser
    fatalsToBrowser)
  • The CGICarp module causes all warnings and
    fatal error messages to be echoed in your browser
    window
  • Use strict (it is optional)
  • This is a standard Perl module that requires us
    to declare all variables.

52
OPERATORS
  • The numeric operator take at least one number as
    an argument and return another number.
  • These operators are of three types
  • ARITHMATIC OPERATOR
  • BITWISE OPERATOR
  • LOGIC OPERATOR

53
ARITHMATIC OPERATOR
  • Adding
  • Dividing
  • multiplying
  • Subtracting
  • Exponentiation
  • Remainder (modulus)
  • Etc

54
(No Transcript)
55
Precedence
  • The order in which PERL do calculations.
  • Multiply and divide have higher precedence than
    addition and subtraction.
  • The outcome should be 108

56
  • For lower precedence
  • Use brackets.
  • But it would give you error.
  • Sowrite like this ..

57
Exponentiation
  • Which raises one number to the power of another.
  • Denoted by
  • The output will be 16, 243, -16
  • Here in the last output precedence would be
    and -.

58
Remainder
  • This calculates remainder when one number divides
    another.

59
Perl assignment operators
  • Write a program using simple numeric operator.
  • Write program in which perl show precedence

60
BITWISE OPERATOR
  • Binary for 51
  • Binary for 170
  • Binary for 85
  • The and operator compare bits and gives the
    output written as .
  • The output will be 17 (it compare one bit at a
    time)

61
  • The or operator compare any of the bit is
    one. Denoted by .
  • Eg if either of the bit is one we produce one
    otherwise 0.
  • As a rule of thumb X Y will always be smaller
    or equal to the smallest value of X and Y. And X
    Y will be bigger than or equal to the largest
    value of X or Y

62
  • The exclusive or operator
  • Denoted by .
  • The nor operator
  • Denoted by .
  • Eg 85.
  • The output would be

63
COMPARING NUMBER FOR EQUALITY
  • Denoted by .
  • If true it gives one otherwise nothing.

COMPARING NUMBER FOR NOT EQUALITY
64
COMPARING NUMBER FOR INEQUALITY
65
Some other operators
  • It does not return true or false value
  • Instead 0 if values are same, -1 if right
    hand side is bigger, 1 if left hand side is
    bigger.
  • Denoted by lt gt.

66
LOGIC OPERATOR(BOOLEAN OPERATOR)
  • AND ()
  • OR ()
  • NOT (!)
  • XOR

67
Lazy evaluation of perl
68
String operators
  • Concatenation operator
  • Repetition operator
  • Repitition has higher precedence than
    concatenation

69
SOME OTHER EXAMPLES
  • Now what happens here

70
String comparison
  • Getting ASCII value of characters.
  • Generally PERL compares by ASCII values of each
    character.
  • To find out the ASCII value
  • Use ORD function
  • E.g.
  • !perl/bin/perl w
  • Print here is the ASCII value of , ord()

71
  • But for comparing whole string (it would be
    tedius)
  • So perl uses some comparison operators
  • Greater than gt
  • Less than lt
  • Equal to eq
  • Not equal ne

72
example
73
Autoincrement/ autodecrement operator
  • These are the operators which increment or
    decrement value according to their position.
  • When they precedes variable they act before
    everything else.
  • When they come after a variable they act after
    everything else.

74
(No Transcript)
75
SCOPE of variable
  • Global and local variable

76
Variable interpolation
  • Single and double quote.

77
Currency conversion
  • !/xampp/usr/bin/perl w
  • Use strict
  • My dol 50
  • Print one dollar equals to , 1dol, \n
  • Using standard input
  • Print Please enter ur value
  • valltstdingt
  • Print one dollar equals to , 50val, rs.

78
Write a program
  • Write a program which convert a hexadecimal
    number to decimal number and a octal number to
    decimal number.
  • Write a program which ask for a decimal number
    which is less than 256 and convert it to binary.
  • Write a program to find out the answer of the
    following

79
list
  • Giving any argument to print function is an
    example of list
  • E.g
  • Print (one , two, three, four)

80
Accessing list value
  • Print ((one, two, three) 2))
  • Another way to do it
  • If you use qw u can separate the words using
    whitspace , tabs and new lines.

81
List slices
  • print (('one', 'two', 'three','four','five')1,3)
  • Another way to do it

82
Ranges
  • print (('one', 'two', 'three','four','five')0..3
    )
  • Perl can count reverse too.
  • print (('one', 'two', 'three','four','five')-1..2
    )
  • The first ouput would be one, two three four.
  • The second output would be five, one, two, three

83
  • Few eaasy examples
  • print "counting up" , (1.. 6), "\n"
  • print "counting down" , (6 .. 1),"\n"
  • print "counting up" , (-1.. 6),"\n"
  • print "counting up" , reverse(1... 6),"\n"
  • print "alphabates ", ('a' .. 'm')

84
Combining ranges and slices
  • Three examples to do it

85
Arrays
  • Started with _at_ sign.

86
Two ways to right an array
  • _at_arr(one, two, three, four)
  • or
  • _at_arrqw(one two three four)

87
Examples of declaring arrays
88
Wanna count array
  • Assign it to a variable..
  • E.g.
  • My count _at_arr
  • Print count
  • Or u can just write
  • Print scalar _at_arr

89
List context and scalar context
  • Operations which want list are list context
  • Operations which want single value are scalar
    context
  • The value of an array in list context is list of
    elements
  • The value in scalar context is its number.

90
Array variable interpolation
  • _at_arr qw(1 2 3 4 5 6)
  • Print _at_arr
  • Print _at_arr
  • The output will be
  • 123456
  • 1 2 3 4 5 6

91
Accessing values in array
  • my _at_arrqw(one two three four)
  • Print arr0
  • Print arr1
  • Print arr2
  • Print arr3

92
(No Transcript)
93
Running through array
Here introduces concept of iterator variable or
loop variable
We can also do it by using _ special variable
94
Another easy way to write it
  • !/strawberry/perl/bon/perl -w
  • use strict
  • my _at_questions qw(java perl C)
  • my _at_ans qw(good best no)
  • for (0..questions)
  • print "the questions_"
  • print "is ans_"

is shows the last element or highest element
95
If u want to reverse ur array values
  • my _at_rev(1..5)
  • for (reverse(_at_rev))
  • print "_...., \n"
  • print "blast off"

96
Pushing and poping of array
  • Push is the operator tht add an element to the
    end of the array.
  • Pop is the operator which remove top element

97
example
  • my _at_listqw(neha dhiraj sonu monu)
  • print "here is list _at_list"
  • my one pop_at_list
  • print "i m picking one","\n"
  • my twopop _at_list
  • print "now i m picking two", "\n"
  • print "_at_list", "\n"
  • my _at_list1push (_at_list, one)
  • print "_at_list", "\n"
  • my _at_list1push (_at_list, two)
  • print "_at_list"

98
Unshift/shift
  • my _at_shi()
  • unshift (_at_shi, "first")
  • print "_at_shi", "\n"
  • unshift (_at_shi, "second", "third")
  • unshift (_at_shi, "forth", "fifth")
  • print "_at_shi","\n"
  • shift _at_shi
  • print _at_shi

99
Sorting data (alphabetically and numerically)
  • my _at_listtqw(java perl python)
  • my _at_listt1 sort_at_listt
  • print _at_listt1
  • Numeric comparison
  • my _at_listtqw(1 56 78 3 5 67)
  • my _at_listt1 sort (_at_listt)
  • print _at_listt1

100
hashes
  • Hashes are like a dictionary in which all entries
    are stored unordered manner.
  • In hash the data is totally unsorted and there is
    no intrinsic order
  • A hash contains pairs of data with no particular
    order and all scattered.

101
Writing a hash
  • There are two ways to write hahses
  • The first way is
  • The second way is

102
  • In the above example we used gt operator.
  • It acts like quoting comma,
  • It is essentially a comma but whatever appear on
    the left side of it , treated as double quoted
    string.
  • See the first value Garry gt dallas
  • The value at the lest side of the operator is key
    and the key at the right side is its value.
  • We use keys to retrieve the value.
  • Hashes keys must be unique, u can not have more
    than one entry for the same name., other wise it
    will be over written.

103
Converting values between arrays and hashes
  • my _at_name qw (neha barve dheeraj kumar kshama
    masihi vibha maltare shradha gavshinde)
  • print "_at_name", "\n"
  • my name1 _at_name
  • print name1kshama
  • my _at_newarray name1
  • print "_at_newarray"
  • Output
  • 1st output neha barve dheeraj kumar kshama
    masihi vibha maltare shradha gavshinde.
  • 2nd output masihi
  • 3rd output shradha gavshinde vibha maltare
    kshama masihi dheeraj kumar neha barve

104
Flipping the hashes
  • If you want to retrieve ur hash by using value,
    use reverse function.
  • my name (neha gt"barve", dheeraj gt
  • "kumar", kshamagt "masihi", vibhagt
  • "maltare", shradhagt "gavshinde")
  • My rev reverse name
  • This will interchange the hash keys into values.

105
Adding changing and taking value away from hashes
  • Adding value
  • phn aabha "sharma"
  • print "aabhas last name is phnaabha", "\n"
  • my _at_assign phn
  • print _at_assign ", "\n"
  • Changing value
  • phn aabha joshi"
  • print "aabhas last name is phnaabha", "\n"
  • my _at_assign phn
  • print _at_assign ", "\n"

106
Removing entry from hash
  • my name (neha gt"barve", dheeraj gt
  • "kumar", kshamagt "masihi", vibhagt
  • "maltare", shradhagt "gavshinde")
  • Delete namekshama

107
Accessing multiple values
  • Using for loop
  • my name (neha gt"barve", dheeraj gt
  • "kumar", kshamagt "masihi", vibhagt
  • "maltare", shradhagt "gavshinde")
  • For (keys name)
  • Print _ \last name is name_, \n
  • You can also use Keys for counting an hash.

108
  • Store your phn numbers in hash. Write a program
    to look up numbers by the persons name.

109
Loops and Decision
  • Control structure which regulate flow of
    execution.

110
The if statement
  • Lets have this example
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my (from, to, value, rate)
  • my rates qw(pounds 1 dollars 1.6
  • marks 3.0 french 10 yen 174.8 swiss 2.43 euro
    1.5)
  • print "enter the currency name u want to
    convert"
  • to ltSTDINgt
  • print "enter the currency name u want to convert
    into"
  • from ltSTDINgt
  • print "enter ur value"
  • valueltSTDINgt
  • chomp (from, value, to)
  • rate ratesfrom/ratesto
  • print "value of value in from is",
  • valuerate,"\n"

111
  • But what if value entered by user doesnt exist
    in hashes
  • Here we use if loop

112
  • Lets have this example
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my (from, to, value, rate)
  • my rates qw(pounds 1 dollars 1.6
  • marks 3.0 french 10 yen 174.8 swiss 2.43 euro
    1.5 rs 50)
  • print "enter the currency name u want to convert
    into"
  • from ltSTDINgt
  • print "enter the currency name u want to
    convert"
  • to ltSTDINgt
  • print "enter ur value"
  • valueltSTDINgt
  • chomp (from, value, to)
  • if (not exists ratesto) die "worng entry
    to"
  • if (not exists ratesfrom) die "wrong
    enrtry from"
  • rate ratesfrom/ratesto
  • print "value of value in to is",
  • valuerate,"\n"

113
Actual if is like tht
  • if (not exists ratesto)
  • die "wrong entry to"
  • if (not exists ratesfrom)
  • die "wrong enrtry from"

114
How it works
  • By using IF statement we check that If the
    currencies are valid entries in the hash.
  • Die is a way of making perl print out an error
    message and finish the program.
  • The part in bracket (not exists ratesto) is
    known as condition, if this condition Is true we
    do the action in braces and terminate the program.

115
  • Another program using operators

116
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my target 50
  • print "guess my number \n"
  • print "enter ur guess"
  • my guessltSTDINgt
  • if (targetguess)
  • print "here u r\, u guessed correctly"
  • if (targetgtguess)
  • print "nop ur number is smaller than me."
  • if (targetltguess)
  • print "nop ur number is bigger than me."

117
Comparing string
118
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my password"hi"
  • print "enter password"
  • my guesltSTDINgt
  • chomp gues
  • if (password eq gues)
  • print "hello my friend"
  • if (password ne gues)
  • print "go away"

119
If condition operator
  • Exists
  • Not exists
  • Defined (whether variable are defined or not..)

120
  • How will u make sure that user didnt leave
    string empty. See the part of program
  • my target 50
  • print "guess my number \n"
  • print "enter ur guess"
  • my guessltSTDINgt
  • if (guess)
  • print please enter ur value"
  • if (targetguess)
  • print "here u r\, u guessed correctly"

121
Unless loop
  • Another way of saying if(not exists)

122
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my (from, to, value, rate)
  • my rates qw(pounds 1 dollars 1.6 marks 3.0
    french 10 yen 174.8 swiss 2.43 euro 1.5)
  • print "enter the currency name u want to convert
    into"
  • from ltSTDINgt
  • print "enter the currency name u want to
    convert"
  • to ltSTDINgt
  • print "enter ur value"
  • valueltSTDINgt
  • chomp (from,value,to)
  • unless (exists ratesto)
  • die "worng entry to"
  • unless (exists ratesfrom)
  • die "wrong enrtry from"

123
  • Write a program to design currency converter
    using unless loop to check whether user has
    entered correct information or not.

124
Statement modifier
  • We can write above coding like this
  • Die u have entered wrong value in to
  • Unless exists(ratesto)
  • Die u have entered wrong value in from
  • Unless exists(ratesfrom)
  • Or
  • Die u have entered wrong value
  • If not exists(ratesto)
  • Die u have entered wrong value
  • If not exists(ratesfrom)

125
If else loop
  • my password"hi"
  • print "enter password"
  • my guesltSTDINgt
  • chomp gues
  • if (password eq gues)
  • print "hello my friend"
  • else
  • print "go away"

126
ifelsif
  • !/strawberry/perl/bin/perl -w
  • use strict
  • print "how is whether out side\n"
  • my whetherltSTDINgt
  • print "what is temp outside"
  • my templtSTDINgt
  • chomp(whether, temp)
  • if (whether eq "snow")
  • print "well we can go"
  • elsif (whether eq "rainy")
  • print "make a plan for tomorrow"
  • elsif (temp gt 20)
  • print "this is good to go outside"
  • else
  • print "no way"

127
Using and operator
  • !/strawberry/perl/bin/perl -w
  • use strict
  • print "how is whether out side\n"
  • my whetherltSTDINgt
  • print "what is temp outside"
  • my templtSTDINgt
  • chomp(whether, temp)
  • if (whether eq "snow" temp lt 30)
  • print "well we can go"
  • else
  • print "no way"

128
For/foreach loop
  • A simple example
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my _at_arraqw(1 2 3 4 5 6)
  • for my i(_at_arra)
  • print i
  • print "\n"

129
Selecting a iterator
  • It is variable which can be selected inside or
    outside of the loop.
  • E.g i

130
Finite and indefinite loop
  • A finite loop will always iterate for each item
    in the array. While indefinite loop check the
    condition and do the action.
  • For loop is definite loop
  • While loop in indefinite

131
While loop
  • While check the condition and perform action if
    it is true.
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my count0
  • while(countlt5)
  • print "here i m \n".count
  • count

132
Backward translation using reverse function and
while loop
  • while(ltSTDINgt)
  • chomp
  • die not done!\n" unless _
  • my sdrawkcabreverse _
  • print "sdrawkcab\n"

133
Do while
134
Until loop
  • Opposite of while loop.
  • my count5
  • until(count0)
  • print "count count\n"
  • count--

135
Controlling loop flow
  • my stop0
  • until(stop)
  • _ltSTDINgt
  • chomp
  • if(_)
  • print my backreverse(_)
  • else
  • stop1
  • print "end\n"
  • (when _ becomes empty string, the if test fails
    and the control is transferred to else, which set
    flag to 1 and until loop stops.)

136
Making an array using flag
  • !/strawberry/perl/bin/perl -w
  • use strict
  • !usr/bin/perl -w
  • _at_arr()
  • my stop0
  • until(stop)
  • _ltSTDINgt
  • chomp
  • if(_)
  • push(_at_arr, _)
  • else
  • stop1
  • print "_at_arr"

137
Breaking out statement
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my _at_array("red","green","stop","blue")
  • for (_at_array)
  • last if _ eq "stop"
  • print "today's colour is _\n"

138
Going onto next statement
139
Regular expressions
  • These are the expressions to find out patterns in
    paragraph, in a sentences, or in whole file.
  • A simple program
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my word"hello there, how are you "
  • if(word/people/)
  • print "yes i found it."
  • else
  • print "nop i cant"
  • The output will be nop I cant.

Some imp tips Regular exp are embedded in / /
slashes. And for comparing a value operator is
used
140
Patterns to use
  • If(_ //)
  • Unless(_//)
  • Or
  • If(_ !//)
  • RE are very sensitive they match string from
    start to end they consider comma, white spaces,
    capitals and the whole word until specified.

141
Few examples which are not considered
142
(No Transcript)
143
Perl insensitivity
  • We can modify this sentense by putting I at the
    end of closing slash.

144
Interpolation of RE
  • !/strawberry/perl/bin/perl -w
  • use strict
  • _"we all are students of bioinformatics"
  • print "enter ur word of choice"
  • my choiceltSTDINgt
  • chomp(choice)
  • if(/choice/)
  • print "here u typed choice"
  • else
  • print "sorry word not found"
  • here we didnt use but RE interpret it this
    is called interpoation just like double quoted
    string.

145
Metacharecters
  • Just like in double quote if u want to turn off
    effect of special characters, use \.
  • Eg.
  • If u want to search in a string
  • Or some special charectors like , , - _at_ ,?
    Etc.
  • By using escape sequence
  • \Q removes effect of all 14 special character.
  • Anything which follows \Q loses its meaning.
  • Opposite of \Q is \E.
  • Eg if(/\Qpattern\E/)

146
(No Transcript)
147
ANCHORS
  • To match the character at the beginning of the
    string and at the end of the string.
  • Two types of anchors
  • anf
  • appear at the beginning of the pattern and
    match the beginning of the string.
  • appears at the end of the pattern and match
    end of the string.
  • E.g
  • In PIR format the sequence should be ended with
    .
  • So we can match the pattern like this.
  • \.

148
coding
  • ! Xampp/perl/bin/perl w
  • Use strict
  • While(ltgt)
  • If (/\/)
  • Print correct sequence
  • else
  • Print please enter PIR format only
  • Here is my input format
  • A T C T
  • Save this document naming pir.txt.
  • Now on command prompt
  • Perl hello.pl pir.txt

149
Similarly for matching pattern at the beggining
  • ! Xampp/perl/bin/perl w
  • Use strict
  • While(ltgt)
  • If (/gt/)
  • Print correct sequence
  • else
  • Print please enter Fasta format only
  • Here is my input format
  • gt A T C T
  • Save this document naming fasta.txt.
  • Now on command prompt
  • Perl hello.pl fasta.txt

150
Matching pattern from a file
  • If u want to match pattern in a file.
  • Use while loop
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my word"ing"
  • while(ltgt)
  • print if /word/
  • ( open it using try.txt file)

151
How it works
  • While (ltgt) opens a file specified in command
    prompt and loops through it line one by one
    feeding each one into special variable _.
  • perl RE ignore new line character in each
    word.

152
Character classes
  • To find out the pattern which are pretty much
    irregular, we use character classes.
  • We put character we consider acceptable inside
    square bracket.
  • E.g. wordpeel of mango.
  • Print enter the word u want to find
  • The into can be like this peiel.
  • This means that the character in may be followed
    by e or i.

153
(No Transcript)
154
\w metacharacter
  • To find out exact word pattern in a string
  • wordno one can make u feel inferior without
    your consent
  • (no to find whether the above line contain 4
    letter word)
  • If(/\s\w\w\w\w\s/)
  • ..
  • (but only \w\w\w\w is wrongly interpreted)

155
Perl boundary (\b)
  • \b it matches the point between smthing tht is
    not a character.
  • It matches the word pattern which are
    specifically embedded by certain punctuations.
    Like( , )
  • wordno one can make u feel inferior without
    your consent
  • If (/\b\w\w\b/)
  • ..

156
. metacharacter
  • To give particular range to pattern.
  • wordno one can make u feel inferior without
    your consent
  • If (/i..er/)
  • ..

157
class character (negation of character class)
  • If we want that given sequence of character
    should not be present in word.
  • E.g.
  • word the principal shared nice thought
  • Now
  • Print enter the word u do not want to match
  • This my be theo
  • The th word should by neither e nor o
  • Similarly

158
Hyphen character class(-)
  • To find out the range of certain pattern
  • E.g.
  • 0-9 to find out numerals in the string literal
  • a-z to find out lower case letter.
  • 0-9a-z digit followed by any alphabet.
  • a-z0-9 alphabet followed by any digit.
  • 0-9A-F to find out any hexadecimal number.
  • 0-9A-Fa-f hexadecimal with lower case.

159
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my (from, to, value, rate)
  • my rates qw(pounds 1 dollars 1.6 marks 3.0
    french 10 yen 174.8 swiss 2.43 euro 1.5)
  • print "enter the currency name u want to convert
    into"
  • from ltSTDINgt
  • print "enter the currency name u want to
    convert"
  • to ltSTDINgt
  • print "enter ur value"
  • valueltSTDINgt
  • chomp (from,value,to)
  • unless (exists ratesto)
  • die "worng entry to"
  • unless (exists ratesfrom)
  • die "wrong enrtry from"

160
Statement modifier
  • We can write above coding like this
  • Die u have entered wrong value
  • Unless exists(ratesto)
  • Die u have entered wrong value
  • Unless exists(ratesfrom)
  • Or
  • Die u have entered wrong value
  • If not exists(ratesto)
  • Die u have entered wrong value
  • If not exists(ratesfrom)

161
If else loop
  • my password"hi"
  • print "enter password"
  • my guesltSTDINgt
  • chomp gues
  • if (password eq gues)
  • print "hello my friend"
  • else
  • print "go away"

162
ifelsif
  • !/strawberry/perl/bin/perl -w
  • use strict
  • print "how is whether out side\n"
  • my whetherltSTDINgt
  • print "what is temp outside"
  • my templtSTDINgt
  • chomp(whether, temp)
  • if (whether eq "snow")
  • print "well we can go"
  • elsif (whether eq "rainy")
  • print "make a plan for tomorrow"
  • elsif (temp gt 20)
  • print "this is good to go outside"
  • else
  • print "no way"

163
Using and operator
  • !/strawberry/perl/bin/perl -w
  • use strict
  • print "how is whether out side\n"
  • my whetherltSTDINgt
  • print "what is temp outside"
  • my templtSTDINgt
  • chomp(whether, temp)
  • if (whether eq "snow" temp lt 30)
  • print "well we can go"
  • else
  • print "no way"

164
For/foreach loop
  • A simple example
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my _at_arraqw(1 2 3 4 5 6)
  • for my i(_at_arra)
  • print i
  • print "\n"

165
Selecting a iterator
  • It is variable which can be selected inside or
    outside of the loop.
  • E.g i

166
Finite and indefinite loop
  • A finite loop will always iterate for each item
    in the array. While indefinite loop check the
    condition and do the action.
  • For loop is definite loop
  • While loop in indefinite

167
While loop
  • While check the condition and perform action if
    it is true.
  • !/strawberry/perl/bin/perl -w
  • use strict
  • my count0
  • while(countlt5)
  • print "here i m \n".count
  • count

168
Backward translation using reverse function and
while loop
  • while(ltSTDINgt)
  • chomp
  • die "!enod\n" unless _
  • my sdrawkcabreverse _
  • print "sdrawkcab\n"

169
Do while
  • To be continues..
Write a Comment
User Comments (0)
About PowerShow.com