1.69k likes | 1.88k Views
PERL : Practical Extraction Reporting Language. Neha Barve Lecturer Bioinformatics Devi Ahilya University. Perl stands for Practical Extraction and Report Language. Perl is known as a high-level, interpreted, general-purpose and dynamic programming language.
E N D
PERL : Practical Extraction Reporting Language NehaBarve Lecturer Bioinformatics Devi Ahilya University
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.
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.
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.
Source code/Code • The instruction that we give to the computer are collectively called as Source Code.
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.
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.
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.
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.
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.
Data types in PERL • Scalars • Arrays (lists) • Associative arrays (hashes)
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. $num=10 $num=10.5 $num=“hello all how r u?”
autovivication • #!usr/bin/perl -w • use strict; • use warnings; • my $diameter=4; • my $circumference; • my $pie; • print "hello world"; • print ( $circumference=$diameter*$pie); • Something which is not defined and perl consider it as “zero”
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
Alternative delimiters $string=q(hi all); $string1=q{hi all}; $string2=q<hi all>; $string3=q[hi all]; $string3=q#hi all#; $string3=q[hi 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
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
Here documents • End of File
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)
Few examples Print “C:\\win\\bun\\perl\\”; Print ‘C:\\win\\bun\\perl\\’; Now what the heck is that?
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
Some examples are: Print 255; Print 0378 Print 0b11111111; Print 0xFF Question: what are Barewords (series of characters that perl doesn’t recognize) Related errors: Illegal octal digit Illegal binary digit
Converting numbers (use of printf or sprintf) • Finding a octal number: $num=42; 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);
Converting decimal to binary • Printf(“%b”, 10); (output: 1010) • Converting binary to decimal $io=sprintf ("%b",10); $hh=oct("0b". $io); print $hh;
<STDIN> INPUT function • print "gimme some number"; • $one=<STDIN>; • #chomp $one; • #print oct($one);
concatenation • String concatenation uses the period character ".“ • my $fullname = 'mud' . "bath"; • Print “$fullname”;
CGI-PERL NehaBarve Lecturer Bioinformatics DAVV
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.
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.
Static Pages Request file Retrieve file Send file
Dynamic Pages Request service Do Computation Generate HTML page with results of computation Return dynamically generated HTML file
CGI Web Application Request service Run CGI program … … … print $result HEADERS BODY
What do u need? • A text editor • A web server • Perl interpreter
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";
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
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
Now write ur second program #!/usr/bin/perl -wT print "Content-type: text/html\n\n"; print "<html><head><title>HelloWorld</title></head>\n"; print "<body>\n"; print "<h2>Hello, world!</h2>\n"; print "</body></html>\n"; (This program shows you tht how u combine HTML, PERL and CGI)
Lets try our third CGI program #!/usr/bin/perl -w print "Content-type: text/html\n\n"; print <<EndOfHTML; <html><head><title>Test Page</title></head> <body> <h2>Hello, world!</h2> </body></html> EndOfHTML
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.
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.
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);
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
The CGI.pm module has many functions; for now we'll start by using these three: • header; • start_html; • end_html;
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;
The start_html; function • start_html prints out the <html>, <head>, <title> and <body> 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”);
The output will be (in CMD) <html> <head> <title>Hello World</title> <head> <body> Giving arguments in Start_html function: #! C:/perl/bin/perl Use CGI qw(:standard); Print header; Print start_html(-title=> “page”, -bgcolor=> “#cccffg”, -text=> “#ggghhh” –background=> “xyz.jpg”);
The end_html; function • The end_html function prints out the closing HTML tags: • </body> </html>
A final program #!/usr/bin/perl -w use CGI qw(:standard); print header; print start_html("Hello World"); print "<h2>Hello, world!</h2>\n"; print end_html; (u can also include all line in one print function)
USING SCALARS IN CGI-PERL #!perl/usr/bin/perl Use CGI qw(:standard); Use CGI::carp qw(warningsToBrowserfatalsToBrowser); Use strict; my $email=“xyz@gmail.com”; my $domain=“www.xyz.com”; Print start_html(“scakars”); Print <<endofhtml <h2>these are scalars</h2> <p> My e-mail address is $email, and my web url is <a href="$url">$url</a>. </p> Endofhtml Print end_html;