Perl Variables Scalar ($) Array (@) Hash (%)
Total Page:16
File Type:pdf, Size:1020Kb
Practical Extraction and Report Language « Perl is a language of getting your job done » « There is more than one way to do it » Larry Wall VI, October 2006 Page 1 Perl Outline : History Structure of a simple Perl script Perl variables scalar ($) array (@) hash (%) Operators (numeric, string & logical) Statement modifiers (if/elsif/else, for/foreach, while) VI, October 2006 Page 2 Practical Extraction and Report Language http://perl.oreilly.com " Perl is both a programming language and an application on your computer that runs those programs " VI, October 2006 Page 3 Perl history A few dates: 1969 UNIX was born at Bell Labs. 1970 Brian Kernighan suggested the name "Unix" and the operating system we know today was born. 1972 The programming language C is born at the Bell Labs (C is one of Perl's ancestors). 1973 “grep” is introduced by Ken Thompson as an external utility: Global REgular expression Print. 1976 Steven Jobs and Steven Wozniak found Apple Computer (1 April). 1977 The computer language awk is designed by Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan (awk is one of Perl's ancestors). VI, October 2006 Page 4 Perl history 1987 Perl 1.000 is unleashed upon the world NAME perl | Practical Extraction and Report Language SYNOPSIS perl [options] filename args DESCRIPTION Perl is a interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). It combines (in the author's opinion, anyway) some of the best features of C, sed, awk, and sh, so people familiar with those languages should have little difficulty with it (Language historians will also note some vestiges of csh, Pascal, and even BASIC|PLUS). Expression syntax corresponds quite closely to C expression syntax. If you have a problem that would ordinarily use sed or awk or sh, but it exceeds their capabilities or must run a little faster, and you don't want to write the silly thing in C, then perl may be for you. There are also translators to turn your sed and awk scripts into perl scripts OK, enough hype. VI, October 2006 Page 5 Perl history 1994 Perl5: last major release (Currently Perl 5.8.6). 1996 Creation of the CPAN repository of modules and documentation ( Comprehensive Perl Archive Network). 2006 Perl 5.8.8 Supported Operating Systems: Unix systems / Macintosh (OS 7-9 and X) / Windows / VMS Perl Features Perls database integration interface (DBI) supports thirdparty databases including Oracle, Sybase, Postgres, MySQL and others. Perl works with HTML, XML, and other markup languages . Perl supports Unicode. Perl is Y2K compliant. Perl supports both procedural and objectoriented programming. Perl interfaces with external C/C++ libraries through XS or SWIG. Perl is extensible There are over 500 third party modules available from (CPAN). VI, October 2006 Page 6 Perl history Perl and the Web Perl is the most popular web programming language due to its text manipulation capabilities and rapid development cycle. Perl's CGIpm module, part of Perl's standard distribution, makes handling HTML forms simple. Perl can handle encrypted Web data, including ecommerce transactions. Perl can be embedded into web servers (mod_perl) to speed up processing by as much as 2000%. Perl's DBI package makes webdatabase integration easy. VI, October 2006 Page 7 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ; The first line of a Perl program is called "command interpretation" or "Shebang line". This line refers to the "#!" and tells the computer that this is a Perl program. To find out whether you should use /usr/bin/perl OR /usr/local/bin/perl, type: "which perl" in your shell: pcX: vioannid$ which perl pcY: vioannid$ which perl /usr/bin/perl /usr/local/bin/perl VI, October 2006 Page 8 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl perl script use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ; #!/import/bc2/soft/bin/perl5/perl [embnet01@bc2-linux3 ~]$ which perl /import/bc2/soft/bin/perl5/perl UNIX shell computerX: vioannid$ which perl computerY: vioannid$ which perl /usr/bin/perl /usr/local/bin/perl VI, October 2006 Page 9 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world " print "Hello world" ; #tell the program to exit exit ; use strict; A command like use strict is called a pragma. Pragmas are instructions to the Perl interpreter to do something special when it runs your program. "use strict" does two things that make it harder to write bad software: It makes you declare all your variables, and it makes it harder for Perl to mistake your intentions when you are using subroutines ALL STATEMENTS END IN A SEMICOLON ";" (similar to the use of the period "." in the English language) VI, October 2006 Page 10 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ; use warnings; Comments are good, but the most important tool for writing good Perl is the "warnings". Turning on warnings will make Perl yelp and complain at a huge variety of things that are almost always sources of bugs in your programs. Perl normally takes a relaxed attitude toward things that may be problems: it assumes that you know what you're doing, even when you don't… VI, October 2006 Page 11 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ; Comments All lines starting with "#" are not taken into account in the execution of the program. Good comments are short, but instructive. They tell you things that aren't clear from reading the code. Blank lines or spaces are also not taken into account in the execution of the program. However, they help in the reading of the code. VI, October 2006 Page 12 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ; Print statement: … prints ! By default, the standard output is the shell window from which the program is executed. ALL STATEMENTS ENDS IN A SEMICOLON ";" (similar to the use of the period "." in the English language) VI, October 2006 Page 13 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ; The exit statement: Tells the computer to exit the program. Although not explicitely required in Perl, it is definitely common. VI, October 2006 Page 14 Perl Hello world ! My first program (hello.pl) : #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ; output: Do not forget to make the file executable: vioannid$ chmod a+x hello.pl vioannid$ ./hello.pl Hello worldvioannid$ VI, October 2006 Page 15 Perl Hello world !! Print : #!/usr/local/bin/perl vioannid$ ./hello2.pl Hello use strict; world use warnings; Hello world Helloworld #play with the print statement vioannid$ #words separated by newline print "Hello\nworld\n" ; #words separated by tabs & a final newline print "Hello\tworld\n" ; #usage of the period to cat strings print "Hello"."world"."\n"; Important: #tell the program to exit Unix & all Unix flavors: \n exit ; Mac OS : \r Windows: \r\n VI, October 2006 Page 16 Perl variables and data types Variables are containers which can hold data of a various types Type Character Example Is a name for: Scalar $ $gene_symbol An individual value (number or string) Array @ @sequences A list of values, keyed by number Hash % %descriptions A group of values, keyed by string Subroutine & &align A callable chunk of Perl code Typeglob * *protein Everything named protein VI, October 2006 Page 17 Perl variables - Scalars scalars a single string (of any size, limited only by the available memory), or a number (integers, floating-point numbers), or a boolean (true or false or 0 or 1), or a reference to something The “beauty” of PERL is that you don’t declare your scalar variables to be of a certain data type, Perl figures it out for you ! Depending on the CONTEXT, Perl will automatically interpret them as strings, as numbers, or as boolean values (true or false). Scalar values are always named with '$' (even when referring to a scalar that is part of an array or a hash). The '$' symbol works semantically like the English word "the" in that it indicates a single value is expected. my $variable_1 = "Hello world !\n"; #note the quotes my $variable_two = 30; #note the absence of quotes VI, October 2006 Page 18 Perl variables - Scalars Numeric my $pi = 3.14159; # floating point number my $avogadro = 6.02e23; # scientific notation my $cash = 33651235421; # huge integer my $cash2 = 33_651_235_421; # huge integer with underlines for visibility my $result = 25; # integer Numeric operators 1 + 1 # 1 plus 1, or 2 2.5 - 1.5 # 2.5 minus 1.5, or 1 2 * 6 # 2 times