Session 1 introduction to perl programming

15
Session 1: Perl Introduction RAM N SANGWAN WWW.RNSANGWAN.COM 1

Transcript of Session 1 introduction to perl programming

Page 1: Session 1 introduction to perl programming

Session 1: Perl IntroductionRAM N SANGWAN

WWW.RNSANGWAN.COM

1

Page 2: Session 1 introduction to perl programming

Agenda

• Scripting language

• Major scripting languages

• Perl

• What is Perl?

• Why Perl?

• Why not Perl?

• Perl Basics

• Executing Perl scripts

• Perl Example 1

• Running your program

2

Page 3: Session 1 introduction to perl programming

What is a scripting language?

• Operating systems can do many things

◦ copy, move, create, delete, compare files

◦ execute programs, including compilers

◦ schedule activities, monitor processes, etc.

• A command-line interface gives you access to these functions, butonly one at a time

• A scripting language is a “wrapper” language that integrates OSfunctions

HY439

AUTUMN 20053

Page 4: Session 1 introduction to perl programming

Major scripting languages• UNIX has sh, Perl

• Macintosh has AppleScript, Frontier

• Windows has no major scripting languages

◦ probably due to the weaknesses of DOS

• Generic scripting languages include:

◦ Perl (most popular)

◦ Tcl (easiest for beginners)

◦ Python (Java-like, best for large programs)

HY439

AUTUMN 20054

Page 5: Session 1 introduction to perl programming

Perl

• "Practical Extraction and Reporting Language"

• written by Larry Wall and first released in 1987

• Perl has become a very large system of modules

• name came first, then the acronym

• designed to be a "glue" language to fill the gap betweencompiled programs (output of "gcc", etc.) and scriptinglanguages

• "Perl is a language for easily manipulating text, files andprocesses": originally aimed at systems administrators anddevelopers

Page 6: Session 1 introduction to perl programming

What is Perl?

• Perl is a High-level Scripting language

• Faster than sh or csh, slower than C

• No need for sed, awk, head, wc, tr, …

• Compiles at run-time

• Available for Unix, PC, Mac

• Best Regular Expressions on Earth

Page 7: Session 1 introduction to perl programming

Why Perl?

• Quick scripts, complex scripts

• Parsing & restructuring data files

• CGI-BIN scripts

• High-level programming

• Networking libraries

• Graphics libraries

• Database interface libraries

Page 8: Session 1 introduction to perl programming

Why Perl? Contd..

• Perl is built around regular expressions REs are good for string processing

Therefore Perl is a good scripting language

Perl is especially popular for CGI scripts

• Perl makes full use of the power of UNIX

• Short Perl programs can be very short “Perl is designed to make the easy jobs easy, without making the

difficult jobs impossible.” -- Larry Wall, Programming Perl

8

Page 9: Session 1 introduction to perl programming

Why not Perl?• Perl is very UNIX-oriented◦ Perl is available on other platforms but isn’t always fully

implemented there.

◦ However, Perl is often the best way to get some UNIX capabilitieson less capable platforms

• Perl does not scale well to large programs◦ Weak subroutines, heavy use of global variables

• Perl’s syntax is not particularly appealing

9

Page 10: Session 1 introduction to perl programming

Perl Basics

• Comment lines begin with: #

• File Naming Scheme• filename.pl (programs)

• filename.pm (modules)

• Example prog: print “Hello, World!\n”;

• Statements must end with semicolon• $a = 0;

• Should call exit() function when finished• Exit value of zero means success

• exit (0); # successful

• Exit value non-zero means failure• exit (2); # failure

• Perl is case-sensitive

Page 11: Session 1 introduction to perl programming

Executing Perl scripts

• "bang path" convention for scripts:◦ can invoke Perl at the command line, or

◦ add #!/usr/bin/perl at the beginning of the script

◦ exact value of path depends upon your platform (use "which perl"to find the path)

• one execution method:◦ $ perlprint "Hello, World!\n";CTRL-DHello, World!

• Preferred method: set bang-path and ensure executableflag is set on the script file

Page 12: Session 1 introduction to perl programming

Perl Example 1

12

#!/usr/bin/perl

#

# Program to do the obvious

#

print 'Hello world.'; # Print a message

Page 13: Session 1 introduction to perl programming

Understanding “Hello World”

• Comments are # to end of line

◦ But the first line, #!/usr/local/bin/perl, tells where to find the Perlcompiler on your system

13

Page 14: Session 1 introduction to perl programming

Running your program

• Two ways to run your program:

◦ $ perl hello.pl

◦ $ chmod 700 hello.pl

$ ./hello.pl

14

Page 15: Session 1 introduction to perl programming

Thankyou

15