Introduction To Perl Basics I - University Of Georgia

1y ago
42 Views
2 Downloads
549.02 KB
29 Pages
Last View : 1d ago
Last Download : 3m ago
Upload by : Julia Hutchens
Transcription

Introduction to Perl Basics IGeorgia Advanced Computing Resources Center (GACRC)EITS/UGAZhuofei Hou8/31/2017Introduction to Perl Basics I1

Outline GACRC Perl Overview Run Perl Script Perl Data Types8/31/2017Introduction to Perl Basics I2

GACRC We are a high-performance-computing (HPC) center at UGA We provide to the UGA research and education community anadvanced 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/2017Introduction to Perl Basics I3

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/2017Introduction to Perl Basics I4

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

Perl’s Brief History In the mid-1980s, awk ran out of steam to produce large reports and Low-level C/C 8/31/2017PerlCreator: Larry WallHigh-level shell scripting, awk, grep, sedIntroduction to Perl Basics I6

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 learn8/31/2017Introduction to Perl Basics I7

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 arrayforeach(@lines)# for each item in array @lines{chomp;# remove tailing New Line character from each lineprint " \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 screenIntroduction to Perl Basics I8

Run Perl ScriptOption 1:Option 2: Create a Perl script my script.pl: Create a Perl script my script.pl:#!/usr/bin/perl#!/usr/bin/env perlprint “Hello World!\n”;print “Hello World!\n”; Make my script.pl executable:chmod a x myScript.pl Run my script.pl:./my script.pl8/31/2017 Make my script.pl executable:chmod a x myScript.pl Run my script.pl:./my script.plIntroduction to Perl Basics I9

Run Perl ScriptOption 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.pl8/31/2017you can run it directly on thecommand line:perl –e ‘print “Hello World!\n”;’Introduction to Perl Basics I10

Perl Data Types Scalar Data List Data and Array Scalar and List Context8/31/2017Introduction to Perl Basics I11

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: undef8/31/2017Introduction to Perl Basics I12

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/2017Introduction to Perl Basics I13

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 10Note: When you use . and x , surrounding spaces are suggested! i** 3;# i is 10008/31/2017Introduction to Perl Basics I14

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/2017Introduction to Perl Basics I15

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, ., 10qw / 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/2017Introduction to Perl Basics I16

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/2017Introduction to Perl Basics I17

Array Array Indexing Range:@arr (1, 2, 3, 4, 5);Index: 0 1 2 3 4 Special Array Index: #arr 41st scalar : arr[0] 12nd scalar : arr[1] 23rd scalar : arr[2] 34th scalar : arr[3] 45th 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/2017Introduction to Perl Basics Ilist assignment18

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

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 4push @a, “fred”;# @a is (1, 2, 3, “fred”)push @a, qw ( a b c );# @a is (1, 2, 3, “fred”, “a”, “b”, “c”)1234545@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 2unshift @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/2017Introduction to Perl Basics I123@a20

Array splice on array @a splice @a START, LEN, REPLIST (LEN and REPLIST are optional!)LEN 3@a:@a:001234512345676@b splice @a, 2, 3, REPLISTSTART 2@b:REPLIST:8/31/2017Introduction to Perl Basics I21

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 @a8/31/2017Introduction to Perl Basics I22

Array Common Operations: foreach sort and reverse join and split map8/31/2017Introduction to Perl Basics I23

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/2017Introduction to Perl Basics I24

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/2017Introduction to Perl Basics I25

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 10foreach i ( ) * 2.7sort “Hello” . reverse “Hello” x print Introduction to Perl Basics I26

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 context8/31/2017Introduction to Perl Basics I27

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 arrayforeach i (3){print “now it is i\n”;}# for each element of (3), print messages to screen:# “now it is 3”8/31/2017Introduction to Perl Basics I28

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

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

a group level, or would be more usefully reported at business segment level. In some instances it may be more appropriate to report separately KPIs for each business segment if the process of aggregation renders the output meaningless. For example it is clearly more informative to report a retail business segment separately rather than combining it with a personal fi nancial services segment .