practical extraction and report language perl is a
play

Practical Extraction and Report Language Perl is a language of - PDF document

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


  1. 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

  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

  3. 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

  4. 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

  5. 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 #!/import/bc2/soft/bin/perl5/perl exit ; [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

  6. 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

  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 ; 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

  8. 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 : vioannid$ ./hello2.pl #!/usr/local/bin/perl Hello world use strict; Hello world use warnings; Helloworld vioannid$ #play with the print statement #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: Unix & all Unix flavors: \n #tell the program to exit exit ; Mac OS : \r Windows: \r\n VI, October 2006 Page 16

Recommend


More recommend