Download presentation
Presentation is loading. Please wait.
Published bySydney Hutchinson Modified over 9 years ago
1
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
2
How to run Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. 2 methods: –Run interpreter directly, giving name of perlscript as argument /afs/rpi.edu/home/36/lallip (1) perl myfile.pl –Run as a Unix shell program. First line of program will tell the shell where the Perl interpreter is located –This line is called the “shebang” –#!/usr/local/bin/perl mildly different from the book must be VERY first line – no comments, blank lines preceding
3
One more step… If you choose to use the shebang, you must tell the OS that this is an executable file. Use chmod (see intro to unix slides) Usually only need to give yourself execute permissions. Once it’s executable, type the filename at a prompt, and it runs. This is the preferred method of running Perl
4
Perl Script Structure semicolon ends each simple statement semicolon optional at final line of a block or loop –optional, but very recommended Comments begin with # and extend to end of line –don’t try to use // or /* … */ variables do not need to be declared –unless you put use strict; at beginning of script this also is recommended, but not necessary. Perl will try to figure out what you mean –won’t even give warnings, unless you tell it to –either use warnings; or put –w after shebang or command line –this is VERY Recommended. Homeworks will lose points if they produce warnings.
5
Very basic I/O simple introduction to reading/writing from keyboard/terminal. More advanced (ie, File) I/O will come around in a couple weeks. This will be just enough to allow us to do some examples, if necessary.
6
Output to terminal the print statement. Takes 0 or more arguments. First (optional) argument is the filehandle. –if omitted, prints to STDOUT. Second, third, fourth, etc… arguments are what to print. –if omitted, prints what ever is in variable $_
7
Output examples Hello World program: #!/usr/bin/local/perl print “Hello World\n”; as this is perl, you can put string in paren’s, but you don’t need to (usually – because this is Perl). more examples: –print “My name is $name\n”; –print “Hi ”, “what\’s ”, “yours?\n”; –print 5 + 3; –print ((4 * 4). “\n”);
8
Input from keyboard read line operator: <> –aka “angle operator”, “diamond operator” –Encloses file handle to read from. Defaults to STDIN, which is what we want. $input = <>; –read one line from STDIN, and save in $input (See variable section, later in this presentation); @input = <>; –read all lines from STDIN, and save as array in @input Again, this makes more sense later on…
9
Our First Bit of Magic The Camel will describe several Perl features as “magical”. The <> operator is the first such feature. If you pass the name of a file (or files) as command line arguments to your script, <> does not read from STDIN. Instead, it will automatically open the first file on the command line, and read from that file. When first file exhausted, it opens and reads from next file. When all files exhausted, THEN <> reads from STDIN
10
Chop & Chomp When reading in a line, carriage return (“\n”) is included. Usually don’t want that. chomp will take off the last character of a string, if it is a “\n”. chop takes off last character of a string, regardless of what it is. –Hence, chomp is “safer”. chomp ($foo = <>); –Very common method of reading in one string from input.
11
Variables Three (basic) types of variables. –Scalar –Array –Hash There are others, but we’ll talk about them at a later time….
12
Scalars Scalar = “single value” In C/C++, many many different kinds of single values: –int, float, double, char, bool In Perl, none of these types need to be declared Scalar variable can hold all these types, and more.
13
Scalars All Scalar variables begin with a $ next character is a letter or _ remaining characters: letters, numbers, or _ Variable names can be between 1 and 251 characters in length Ex: $foo, $a, $zebra1, $F87dr_df3 Wrong: $24da, $hi&bye, $bar$foo
14
Scalar Assignments Scalars hold any data type: $foo = 3; $d = 4.43; $temp = ‘Z’; $My_String = “Hello, I’m Paul.”
15
Arrays Concept is the same as in C/C++ –Groups of other values –Groups of scalars, arrays, hashes much more dynamic than C/C++ –no declaration of size, type –can hold any kinds of value, and multiple kinds of values All array variables start with the @ character –@array, @foo, @My_Array, @temp34
16
Array assignments @foo = (1, 2, 3, 4); @bar = (“hello”, “my”, “name”, “is”, “Paul”); @temp = (34, ‘z’, “Hi!”, 43.12); Arrays are 0-indexed, just as in C/C++ $temp[1] ‘z’; –NOTE: This is a *single value*, hence the $ $bar[3] = “was”; –@bar now: (“hello”, “my”, “name”, “was”, “Paul”);
17
Array vs. Scalar $foo = 3; @foo = (43.3, 10, 8, 5.12, “a”); $foo and @foo have *nothing in common*. In fact, $foo has nothing to do with $foo[3]; “This may seem a bit weird, but that’s okay, because it is weird.” –Programming Perl, pg. 54
18
More about arrays special variable for each array: –@foo = (3, 25, 43, 31); –$#foo 3. Last index of @foo. –$foo[$#foo] 31; This can be used to dynamically alter the size of an array: –$#foo = 5; -- creates two null values on the end of @foo –$#foo = 2; -- destroys all but the first three elements of @foo “Slices” – part of an array (or hash) –@bar = @foo[1..3]; @bar (25, 43, 31) –@bar = @foo[0,1]; @bar (3, 25)
19
Join/Split Built-in Perl functions Split – split a string into a list of values –$BigString = “Hello,_I_am_Paul”; –@strings = split ‘_’, $BigString; –@strings (“Hello,”, “I”, “am”, “Paul”); Join – join a list/array of values together –$BigString = join ‘ ’, @strings; –$BigString “Hello, I am Paul”;
20
Hash (somewhat) Analogous to C++ hashtable. aka “Associative Array” – ie, array not indexed by numerical sequence. list of keys and values. All hash variables start with % Use to keep list of corresponding values –TIP: Any time you feel the need to have two separate arrays, and do something with elements at corresponding positions in the arrays (but don’t care where in array elements actually are), USE A HASH
21
Hash example Want a list of short names for months: %months = ( “Jan” => “January”, “Feb” => “February”, “Mar” => “March”, … ); reference by *curly* brackets… –Avoid confusion with array notation $month{“Jan”} “January”;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.