Overview of Perl Programming: Features, Popularity, and Learning

Slide Note
Embed
Share

Perl is a versatile scripting language suitable for various tasks such as test processing, system administration, and bioinformatics. Despite a decline in popularity due to the rise of languages like PHP and Python, Perl remains useful and easily accessible on different platforms. Learning Perl requires understanding basic concepts and practical application to build useful tools. This overview covers the key aspects of Perl, including its strengths, weaknesses, and learning approach.


Uploaded on Sep 21, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Getting Started with Perl Jeffrey ROACH 28 November

  2. What is Perl? What is it good for? Perl is a scripting language Perl is a prototyping language Perl is designed for relatively short scripts Perl programs are best written by a single programmer Perl is ideal for: Test processing: System Administration, Back-end web administration, Bioinformatics

  3. Popularity of Perl

  4. The decline of Perl Replaced by PHP and Python Syntax is very different from other languages Programming constructs programmers expect are non-supported Subroutines are available, but weak Object-oriented techniques are available, but weak and slow Perl is not suitable for large scale, multi- developer projects Perl 6 has been coming next year for the last five years

  5. Nevertheless Perl remains useful as a scripting language Perl is installed in all Linux/Unix/Mac OS X machines Perl is easily installed on Windows (Active State) Perl is free and a good place to start learning good practice Perl allows non-programmers to write small programs that can do worthwhile things quickly

  6. Learning Perl Perl is a big, messy language Two hours is not sufficient to even crack the surface What we will do: Learn seven (7) basic concepts Use this foundation to build some small, useful (at least a little) tools Decide whether you want to learn more on your own: Learning Perl The Hard Way

  7. Getting Started on Kure or Killdevil Step 1: Get the course materials cd /netscr/<your_onyen> cp r /netscr/roachjm/Perl . ls Step 2: Choose an editor nano 01_helloworld.pl vi 01_helloworld.pl emacs 01_helloworld.pl (OR) (OR)

  8. 01_helloworld.pl 01_helloworld.pl #!/usr/bin/perl Notes Indicates which perl to use Allows ./01_helloworld.pl Use chmod u+x <file.pl> Strict and warnings pretty much standard Note distinction between and Note the new line characer \n use strict; use warnings; print "Hello, World!\n"; print 'Hello, World'; print "<--- No New Line\n"; print 'Hello, World!\n'; print "\n";

  9. 02_variables.pl 02_variables.pl Notes Usual start Standard practice #!/usr/bin/perl use strict; use warnings; my $a = 1; print "a: $a\n"; Variables hold values Values may be numbers or strings Perl is pretty promiscuous about this my $b = 2; print "b: $b\n"; $b = $a + $b; print "Added a to b.\n"; print "b: $b\n"; my $first_name = "Jeff"; my $last_name = "ROACH"; print "Full Name: $first_name $last_name\n";

  10. 03_arrays.pl 03_arrays.pl my @a = (1,2,3,4,5); Notes Arrays are variables that hold more than one value Individual values are indexed by an integer Array is @a First element is $a[0] Arrays can store numbers and strings Index must always be integer print "@a\n"; print '$a[0] $a[1] $a[2] $a[3] $a[4]:'; print "\t$a[0] $a[1] $a[2] $a[3] $a[4]\n"; print '$a[-1] $a[-2] $a[-3] $a[-4] $a[-5]:'; print "\t$a[-1] $a[-2] $a[-3] $a[-4] $a[-5]\n"; my $len_a = scalar(@a); for (my $i=0; $i<$len_a; $i++) { print "$a[$i] "; } print "\n";

  11. 04_hashes.pl 04_hashes.pl my %a = ('one'=>1, 'two'=>2, 'three'=>3, 'four'=>4, 'five'=>5); Notes Same as arrays, but with arbitrary indices Hash is %a Element at f is $a{ f } Also called dictionaries, associative arrays, maps print "%a\n"; print '$a{\'one\'} $a{\'two\'} $a{\'three\'} $a{\'four\'} $a{\'five\'}:'; print "\t$a{'one'} $a{'two'} $a{'three'} $a{'four'} $a{'five'}\n\n"; foreach (keys %a) { print "$_ => $a{$_} "; } print "\n\n";

  12. 05_subs.pl 05_subs.pl sub greeting { my @param = @_; Notes Subroutines were the basis for Structured Programming circa 1970 1975 Natural way to break larger programs into smaller blocks Improves readability, code re-use, and code quality Perl support is somewhat underwhelming print "Hello, $param[0], how are you?\n\n"; return 0; } print "Round 1:\n"; foreach (@names) { greeting($_); }

  13. 06_scope.pl 06_scope.pl my @names = ('Jeff', 'Jon', 'David', 'Sam'); Notes Scope is the value that subroutines add Scope restricts the value that variables take to a particular subroutine Essentially a context Prevents name conflicts and allows larger programs to be composed of smaller parts Hierarchical Concept expanded in object- oriented programming sub scope1 { print "In Scope1:\n"; print "\tNames: @names\n"; my @new_names = ('Ffej', 'Noj', 'Divad', 'Mas'); print "\tNew Names: @new_names\n"; }

  14. 07_files.pl 07_files.pl sub read_file { my @params = @_; Notes ./07_files.pl File1.txt File2.txt diff File1.txt File2.txt cp File1.txt File2CP.txt diff file2.txt File2CP.txt my $filename = $params[0]; open(FILE,'<',$filename) or die $!; } print "\nRead File\n"; read_file("File1.txt"); Read and write from files Fundamental importance print "\nWrite File\n"; copy_file("File1.txt","File2.txt");

  15. 08_echo.pl 08_echo.pl Notes #!/usr/bin/perl ./08_echo.pl This is a test echo This is a test use strict; use warnings; my $argc = scalar(@ARGV); Uses @ARGV builtin array print "@ARGV\n\n"; for (my $i=0; $i<$argc; $i++) { print "$ARGV[$i] "; } print "\n\n"; foreach (@ARGV) { print "$_ "; } print "\n";

  16. 09_stats.pl 09_stats.pl Notes ./09_stats.pl 1 2 3 4 5 my $argc = scalar(@ARGV); # You can print "@ARGV\n"; my $sum = 0.0; my $sumsq = 0.0; for (my $i=0; $i<$argc; $i++) { $sum = $sum + $ARGV[$i]; $sumsq = $sumsq + $ARGV[$i]*$ARGV[$i]; } # denotes comments Uses @ARGV builtin array my $mean = $sum / $argc; my $stddev = sqrt(($sumsq - $sum*$sum/$argc) / ($argc - 1)); Single pass standard deviation print "n: $argc\n"; print "mean: $mean\n"; print "stddev: $stddev\n";

  17. 10_cat.pl 10_cat.pl sub read_file { my @params = @_; Notes ./10_cat.pl File1.txt File2.txt cat File1.txt File2.txt my $filename = $params[0]; open(FILE,'<',$filename) or die $!; Basic command line args Basic file I/O while (my $line = <FILE>) { print $line; } close(FILE); return 0; }

  18. 11_wc.pl 11_wc.pl Notes sub count_file { my @params = @_; ./11_wc.pl File1.pl wc File1.pl my $filename = $params[0]; open(FILE,'<',$filename) or die $!; my $characters = 0; my $words = 0; my $lines = 0; Basic file I/O Accumulator String Split while (my $line = <FILE>) { $characters = $characters + length($line); my @word_array = split(' ',$line); $words = $words + scalar(@word_array); $lines = $lines + 1; } Character, word, line order reversed close(FILE); return "$characters $words $lines"; }

  19. 12_cut.pl 12_cut.pl Notes sub cut_file { my @params = @_; ./12_cut.pl File1.csv cut d , -f 1,4 File1.csv my $filename = $params[0]; open(FILE,'<',$filename) or die $!; while (my $line = <FILE>) { Split on comma Print selected columns chomp($line); my @line_array = split(',',$line); my $column1 = $line_array[0]; my $column4 = $line_array[3]; print "$column1,$column4\n"; } close(FILE); return 0; }

  20. 13_grep.pl 13_grep.pl Notes ./13_grep.pl File1.log while (my $line = <FILE>) { chomp($line); Chomp procedure If/then control structure Regular expression if ($line =~ m/System Sleep/) { $times = $times + 1; my @line_array = split(' ',$line); my $date1 = $line_array[0]; my $date2 = $line_array[1]; my $time = $line_array[2]; Log file analysis print "System Sleep at: $date1 $date2 $time\n"; } }

  21. 14_head.pl 14_head.pl Notes sub read_file { my @params = @_; ./14_head.pl File1.log head File1.log my $filename = $params[0]; open(FILE,'<',$filename) or die $!; my $lines_written= 0; while ((my $line = <FILE>) and ($lines_written< 10)) { print $line; $lines_written= $lines_written+ 1; Use of boolean and in while Failed use of break Common use of comments #Not allowed with strict #if ($lines_written== 10) { # break; #} } close(FILE); return 0; }

  22. 15_awk.pl 15_awk.pl Notes ./15_awk.pl File2.csv sub hist_file { my @params = @_; my $filename = $params[0]; open(FILE,'<',$filename) or die $!; AWK is actually is own programming String eq not = my %cities = (); while (my $line = <FILE>) { chomp($line); my @line_array = split(',',$line); if ($line_array[4] eq '"Active"') { my $city = $line_array[0]; my $hours = $line_array[5];

  23. 15_awk.pl 15_awk.pl my $city_total = $cities{$city}; if ($city_total) { $cities{$city} = $city_total + $hours; } else { $cities{$city} = $hours; } } } Notes If/Then/Else checking for key Returns cities hash close(FILE); return %cities; }

  24. Conclusions Perl has tons of possibilities Expressive: You can write things a million different ways Useful: You can make useful little tools relatively easily Organic development and prototyping For further self-study: Learning Perl The Hard Way There is also Python, Ruby, PHP, and Lua

Related