Session 1 introduction to perl programming
-
Upload
ram-n-sangwan -
Category
Education
-
view
44 -
download
1
Transcript of Session 1 introduction to perl programming
Session 1: Perl IntroductionRAM N SANGWAN
WWW.RNSANGWAN.COM
1
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
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
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
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
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
Why Perl?
• Quick scripts, complex scripts
• Parsing & restructuring data files
• CGI-BIN scripts
• High-level programming
• Networking libraries
• Graphics libraries
• Database interface libraries
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
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
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
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
Perl Example 1
12
#!/usr/bin/perl
#
# Program to do the obvious
#
print 'Hello world.'; # Print a message
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
Running your program
• Two ways to run your program:
◦ $ perl hello.pl
◦ $ chmod 700 hello.pl
$ ./hello.pl
14
Thankyou
15