120 likes | 241 Views
Introduction to Perl: P ractical e xtraction and r eport l anguage. Origins Nature of Perl Characteristics of Perl Example. What is Perl?. Includes most powerful constructs from predecessor languages (sh, awk)
E N D
Introduction to Perl: Practical extraction and report language • Origins • Nature of Perl • Characteristics of Perl • Example
What is Perl? • Includes most powerful constructs from predecessor languages (sh, awk) • From awk (text file processing utility) Designed to write short programs to process text files, using pattern matching to produce reports of results • From UNIX sh, csh process creation and destruction
What is Perl? • Added sockets networking language to support communication with other processes and languages • Added modules and OOP Added constructs expressive, flexible language
History • Release 1.000 1987 designed by Larry Wall • Began as UNIX tool, but has spread • Popularity increased in 1990s • Portable systems programming language • Easily obtained and free • Useful for Common Gateway Interface (CGI) programming for WWW
Language and Programs • Simple language – a beginner can write small useful programs • Complex language – has a richness and power to perform sophisticated tasks • Easy language to learn if you know C, sh or awk • Practical (easy to use, efficient, complete), but not beautiful • Be disciplined, follow standards PERL programs will be readable
Scripts vs Programs • Early OS/UNIX sh: • Small sequence of commands repeatedly typed put into files and interpreted • Hence scripts • Perl evolved from shell languages • But Perl “scripts” are first compiled into intermediate language before interpreted • Hence Programs
Central Characteristics • Implicit variables - defined by language implementation, not defined by user, but accessible • Variables are implicitly declared – type is inferred by compiler based on name syntax or context • Numbers are numbers - one numeric type, same as double (all literals and operations implicitly converted to this type) • Strings and numbers – implicit type conversions • No unnecessary limits – strings and arrays implicitly grow
Central Characteristics • Scalar and list context • Array var when scalar expected length of array • Scalar var when array expected array of 1 element • Interchangeability of functions and operations • Ex. list operators in which op is followed by operands • Ex. Functions have () around operands, but can omit () • More than one way to do it • Simple techniques and more complex, elegant alternatives
Getting Perl • www.activestate.com or My CD • Four directories needed: • doc – Perl doc (manpages) • src – source code files for Perl • ports – subdirectories and symbolic links for • implementations of Perl not supported by • standard distribution • scripts – collection of example Perl programs
Other tools • Editor – NTEmacs free • www.cs.washington.edu/hones/voelker/ntemacs • Debugger – see ActiveState debugger
Example Program To see listing of environment variables on your computer available to the program #!/usr/local/bin/perl foreach $key (keys (%ENV)) { print qq | The value of $key is ENV{"$key"}\n|; }
Running Example Program • Save as with .pl extension • At command prompt type filename.pl • For verbose warnings type perl –w filename.pl • To run with debugger perl –d filename.pl