Introduction To Perl Basics I - Wiki.gacrc.uga.edu

3m ago
10 Views
0 Downloads
549.02 KB
29 Pages
Last View : Today
Last Download : n/a
Upload by : River Barajas
Transcription

Introduction to Perl Basics I Georgia Advanced Computing Resources Center (GACRC) EITS/UGA Zhuofei Hou 8/31/2017 Introduction to Perl Basics I 1

Outline GACRC Perl Overview Run Perl Script Perl Data Types 8/31/2017 Introduction to Perl Basics I 2

GACRC We are a high-performance-computing (HPC) center at UGA We provide to the UGA research and education community an advanced computing environment: HPC computing and networking infrastructure located at the Boyd Data Center Comprehensive collection of scientific, engineering and business applications Consulting and training services http://wiki.gacrc.uga.edu (GACRC Wiki) https://wiki.gacrc.uga.edu/wiki/Getting Help (GACRC Support) http://gacrc.uga.edu (GACRC Web) 8/31/2017 Introduction to Perl Basics I 3

Perl Overview What does “Perl” Stand for? Perl’s Brief History Tasks Perl is Good and Not Good for Perl’s “Hello, World!” 8/31/2017 Introduction to Perl Basics I 4

What does “Perl” Stand for? Option 1: Practical Extraction and Report Language Option 2: Pathologically Eclectic Rubbish Lister Which would you select? 8/31/2017 Introduction to Perl Basics I 5

Perl’s Brief History In the mid-1980s, awk ran out of steam to produce large reports and Low-level C/C 8/31/2017 Perl Creator: Larry Wall High-level shell scripting, awk, grep, sed Introduction to Perl Basics I 6

Tasks Perl is Good and Not Good for Good for: Optimized for working with text (90%) and everything else (10%) Not Good for: Binary coding, GUI programing, OOP programming Easy to use, but take some time to learn 8/31/2017 Introduction to Perl Basics I 7

Perl’s “Hello, World!” #!/usr/bin/perl # run Perl’s interpreter program perl @lines ls -l ; # outputs of command ls -l are read into @lines, @ defines array foreach(@lines) # for each item in array @lines { chomp; # remove tailing New Line character from each line print " \n" if / d/; # print the line to screen if the begins with 'd‘ (for directory) } print “Hello World!\n”; 8/31/2017 # print a message to screen Introduction to Perl Basics I 8

Run Perl Script Option 1: Option 2: Create a Perl script my script.pl: Create a Perl script my script.pl: #!/usr/bin/perl #!/usr/bin/env perl print “Hello World!\n”; print “Hello World!\n”; Make my script.pl executable: chmod a x myScript.pl Run my script.pl: ./my script.pl 8/31/2017 Make my script.pl executable: chmod a x myScript.pl Run my script.pl: ./my script.pl Introduction to Perl Basics I 9

Run Perl Script Option 3: Option 4: Create a Perl script my script.pl: For a small script with several lines, print “Hello World!\n”; Run my script.pl by calling perl: perl ./my script.pl 8/31/2017 you can run it directly on the command line: perl –e ‘print “Hello World!\n”;’ Introduction to Perl Basics I 10

Perl Data Types Scalar Data List Data and Array Scalar and List Context 8/31/2017 Introduction to Perl Basics I 11

Scalar Data Integer Number: 0, 2017, 255, -42, 786251429018 (you can have long integer!) Floating-Point Number: 1.25, 255.0, 7.31e2, -12e-10, 3.2E5 (default double-precision!) Single-Quoted String: ‘’, ‘fred’, ‘hello, world!\n’, ‘I didn\’t see.’ Double-Quoted String: “”, “barney”, “hello world!\n”, “coke\tsprite” Undefined Scalar Data: undef 8/31/2017 Introduction to Perl Basics I 12

Scalar Data Scalar Variable: variableName, a scalar variable holds a scalar. Perl identifier: Made up of letters, underscores, and digits. Starts with a letter (a-z, A-Z) or underscore ( ) only! Scalar Assignment with , -, *, / (for numbers) and ., x (for strings): i 10; s1 ‘Hello'; x 10.11; s2 “World!” ; y x i; # y is 20.11 s3 s1 . “, ” . s2; # s3 is “Hello, World!” y y * 2; # y is 40.22 s3 s3 x 2; # s3 is “Hello, World!Hello, World!” Note: When you use . and x, surrounding spaces are suggested! 8/31/2017 Introduction to Perl Basics I 13

Scalar Data Scalar Binary Assignment with , - , * , / , ** (for numbers) and . , x (for strings): i 10; s ‘Hello'; i 5; # i is 15 s . “, World!”; # s is “Hello, World!” i* 2; # i is 30 s x 2; # s is “Hello, World!Hello, World!” i- 20; # i is 10 Note: When you use . and x , surrounding spaces are suggested! i** 3; # i is 1000 8/31/2017 Introduction to Perl Basics I 14

Scalar Data Auto Conversion between Numbers and Strings: Perl uses them nearly interchangeably! Numbers or Strings? It is depend on the operator that you are using! Operator Overloading i 10; # i is an integer scalar s “17”; # s is a numeric string scalar i s; # is a numeric operator result is 27 i . s; # . is a string operator result is “1017” s * 2; # * is a numeric operator results is 34 s x 2; # x is a string operator result is “1717” 8/31/2017 Introduction to Perl Basics I 15

List Data List: A collection of scalars. List is a data type! (1, 2, 3, 4, 5); # a list of 5 integer scalars (“fred”, 3.14, 1); # a list of 1 string and 2 number scalars (); # an empty list (1.100); # a list of 100 integer scalars 1, 2, 3, , 100 m 1; n 10; ( m. n); # a list of 10 integer scalars 1, 2, 3, ., 10 qw / fred barney rock / # qw: quoted words, equivalent to (“fred”, “barney”, “rock”) qw / 1 2 3 4 5 / # equivalent to (“1”, “2”, “3”, “4”, “5”) Note: You can choose any punctuation character as the delimiter, e.g., qw ( ) or qw ! ! or qw # # 8/31/2017 Introduction to Perl Basics I 16

Array Array Variable: @arrayName An array variable holds a list, just like a scalar variable holds a scalar. @arr1 (“fred”, 3.14, 1); # @arr1 holds a list of 1 string and 2 number scalars @arr2 (); # @arr2 holds an empty list @arr3 (1.100); # @arr3 holds (1, 2, 3, , 100) @arr5 qw / fred barney rock /; # @arr5 holds (“fred”, “barney”, “rock”) 8/31/2017 Introduction to Perl Basics I 17

Array Array Indexing Range: @arr (1, 2, 3, 4, 5); Index: 0 1 2 3 4 Special Array Index: #arr 4 1st scalar : arr[0] 1 2nd scalar : arr[1] 2 3rd scalar : arr[2] 3 4th scalar : arr[3] 4 5th scalar : arr[4] 5 arr[1] arr[4] ? 5th scalar : arr[ #arr] Quiz 1: How many items in arr? #arr or #arr 1? Quiz 2: What’s the difference? ( arr0, arr1, arr2) (“fred”, 3.14, 1) ( arr[0], arr[1], arr[2]) (“fred”, 3.14, 1) 8/31/2017 Introduction to Perl Basics I list assignment 18

Array Adding and Deleting Elements: 8/31/2017 Introduction to Perl Basics I pop and push shift and unshift splice 19

Array pop and push on @a (1, 2, 3, 4, 5): pop @a; # @a is (1, 2, 3, 4); 5 is discarded v pop @a; # @a is (1, 2, 3); v is 4 push @a, “fred”; # @a is (1, 2, 3, “fred”) push @a, qw ( a b c ); # @a is (1, 2, 3, “fred”, “a”, “b”, “c”) 1 2 3 4 5 4 5 @a shift and unshift on @a (1, 2, 3, 4, 5): shift @a; # @a is (2, 3, 4, 5); 1 is discarded v shift @a; # @a is (3, 4, 5); v is 2 unshift @a, “barney”; # @a is (“barney”, 3, 4, 5) unshift @a, qw ( a b c ); # @a is (“a”, “b”, “c”, “barney”, 3, 4, 5) 8/31/2017 Introduction to Perl Basics I 1 2 3 @a 20

Array splice on array @a splice @a START, LEN, REPLIST (LEN and REPLIST are optional!) LEN 3 @a: @a: 0 0 1 2 3 4 5 1 2 3 4 5 6 7 6 @b splice @a, 2, 3, REPLIST START 2 @b: REPLIST: 8/31/2017 Introduction to Perl Basics I 21

Array splice on @a (“fred”, “barney”, 3, 4, “tom”, 7): @b splice @a, 2, 3, qw( a b ); # @a is (“fred”, “barney”, “a”, “b”, 7) # @b is (3, 4, “tom”) @b splice @a, 2, 3; # @a is (“fred”, “barney”, 7) # @b is (3, 4, “tom”) @b splice @a, 2; # @a is (“fred”, “barney”) # @b is (3, 4, “tom”, 7) Note: Each command line on this slide is operating on the initial array @a 8/31/2017 Introduction to Perl Basics I 22

Array Common Operations: foreach sort and reverse join and split map 8/31/2017 Introduction to Perl Basics I 23

Array foreach: @fruits qw /apple pear orange banana/; # @fruits is (“apple”, “pear”, “orange”, “banana”) foreach fruit (@fruits) # for each element, print messages to screen: { # “fruit is apple” print “fruit is fruit\n”; } # “fruit is pear” # sort and reverse: @sorted sort @fruits # @sorted is (“apple”, “banana”, “orange”, “pear”) @reversed reverse @sorted # @reversed is (“pear”, “orange”, “banana”, “apple”) 8/31/2017 Introduction to Perl Basics I 24

Array join (array string) and split (string array): @fruits qw /apple pear orange banana/; # @fruits is (“apple”, “pear”, “orange”, “banana”) fruitString join “-”, @fruits; # fruitString is “apple-pear-orange-banana” line “John Smith:jsmith:Physics:grad”; @info split “:”, line; # @info is (“John Smith”, “jsmith”, “Physics”, “grad”) map: evaluates an EXPR for each list element and returns the list with evaluated values @negatives (-10.0); # @negatives is (-10, -9, -8, , 0) @positives map abs, @negatives; # @positives is (10, 9, 8, , 0) # abs is one of built-in numeric functions of sqrt, # log, sin, cos, tan, etc. 8/31/2017 Introduction to Perl Basics I 25

Scalar and List Context When Perl is parsing an expression, it’s always expecting either a scalar or a list. What Perl expects is called the context of the expression. How to decide? It is the operation that decides the context, for example: Scalar Context: 8/31/2017 a List Context: @fred arr[3] ( a, b) 123 push @arr, 5 10 foreach i ( ) * 2.7 sort “Hello” . reverse “Hello” x print Introduction to Perl Basics I 26

Scalar and List Context List in Scalar Context: @a qw/1 2 fred barney/; # @a is (“1”, “2”, “fred”, “barney”) n @a; # n is 4 length of @arr #arr 1 s “Hello! ” x @a; # s is “Hello! Hello! Hello! Hello! ” @b reverse @a; # @b is (“barney”, “fred”, “2”, “1”) s reverse @a; # s is “yenrabderf21” s sort @a; # s is undef n scalar @a; # n is 4; built-in function scalar to force scalar context 8/31/2017 Introduction to Perl Basics I 27

Scalar and List Context Scalar in List Context: Scalar value will be automatically used to make a one-element list: @a 1; @a 5 * 7; @a undef; @a (); # @a is ( 1) # @a is (35) # @a is (undef) # @a is an empty array foreach i (3) { print “now it is i\n”; } # for each element of (3), print messages to screen: # “now it is 3” 8/31/2017 Introduction to Perl Basics I 28

Thank You! Basics I: Perl overview, fundamental data types Basics II: programming structures (if, unless, while, until, foreach, for), Input and Output Basics III: Perl Regular Expression, text processing, file test and directory operation 8/31/2017 Introduction to Perl Basics I 29

Run Perl Script Option 3: Create a Perl script my_script.pl: Run my_script.pl by calling perl: 8/31/2017 Introduction to Perl Basics I 10 print Hello World!\n; perl ./my_script.pl Option 4: For a small script with several lines, you can run it directly on the command line: perl -e print Hello World!\n;

Related Documents:

Why Perl? 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,

Run Perl Script Option 3: Create a Perl script my_script.pl: Run my_script.pl by calling perl: 8/31/2017 Introduction to Perl Basics I 10 print Hello World!\n; perl ./my_script.pl Option 4: For a small script with several lines, you can run it directly on the command line: perl -e print Hello World!\n;

Perl can be embedded into web servers to speed up processing by as much as 2000%. Perl's mod_perl allows the Apache web server to embed a Perl interpreter. Perl's DBI package makes web-database integration easy. Perl is Interpreted Perl is an interpreted language, which means that your code can be run as is, without a

Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18, 1987 by Larry Wall. Perl culture Perl Poems BEFOREHAND: close door, each window & exit; wait until time. open spellbook, study, read (scan, select, tell us);

Other Perl resources from O’Reilly Related titles Learning Perl Programming Perl Advanced Perl Programming Perl Best Practices Perl Testing: A Developer’s . Intermedi

Perl's creator, Larry Wall, announced it the next day in his State of the Onion address. Most notably, he said "Perl 6 is going to be designed by the community." Everyone thought that Perl 6 would be the version after the just-released Perl v5.6. That didn't happen, but that's why "Perl" was in the name "Perl 6."

tutorial Sorry about that but I have to keep my tutorial's example scripts short and to the point Finally, this is a tutorial for Perl/Tk only I will not be teaching perl here So if you know perl, continue But if you are a beginner to perl, I would recommend that you read my perl tutorial

INTRODUCTION 5 562, 579, 582, 585, 591, 592, 610). Population genetics, for example, identifies the conditions—selection pressures, mutation rates, population