160 likes | 180 Views
print 'Hello world.';. Perl Basics – Part I. Tren Griffith. Outline:. Perl introduction Scalar Data Variables Operators Control Structures Input Lists and Arrays Comparison Examples. Introduction. Unix scripting language. Developed in 1987 for report (text) processing.
E N D
print 'Hello world.'; Perl Basics – Part I Tren Griffith
Outline: • Perl introduction • Scalar Data • Variables • Operators • Control Structures • Input • Lists and Arrays • Comparison Examples
Introduction • Unix scripting language. • Developed in 1987 for report (text) processing. • Free under GNU Public License. • TMTOWTDI - “Tim Toady” – Perl’s Motto. • There’s more than one way to do it.
Scalar Data & Variables • Two basic Scalar data types. • Numbers • Integer or Floating-point. • undef = 0 • Strings • Sequence of Characters. • Enclosed in either single or double quotes. • undef = Empty string • Each Scalar variable holds a single value. • Names of variables begin with a $ and followed by an identifier. • $Employee $Count
Features: • Any scalar variable inside a double quoted string, is replaced by its value. • Example: • $string="world"; print "hello $string \n"; • The example will print “hello world” and switch to a new line
Features II: • There is automatic conversion between numbers and strings. • Examples:$fred=10;$barney=5; • Print $fred*$barney; #Prints 50. • As the * is a arithmetic operator, it multiplies. • Print $fred x $barney; #Prints 1010101010 • Since x is a string operator, Perl interrupts this as “10 five times” • Print $fred . $barney; #Prints 105 • Since . (dot) is a string operator, Perl interrupts this as “10 and 5”
Features III: • Automatic conversion between numbers and strings: In practice. • In C++ - If you needed to use an integer as a string… • std::string IntToStr( int n ) { std::ostringstream result; result << n; return result.str(); } • In Perl – If you need to use an integer as a string… Just use it. No conversion needed.
Control Structure • If Statement: • if(…){…} • If Else Statement: • if(…){…} else { • …} • While Statement • while(…){…}
Control Structure II • While Very Similar to C++, however braces are required in all cases. • In C++: • while(…)cout << “Hello”; • It is legal to omit braces in C++. • In Perl: • while(…) { $counter++} • Program would fail without the braces.
Operators • Assignment Operator: = • Arithmetic Operator: + - * / • String Operator • . Concatenation • X Repetition • Numeric Comparison: • == (Equal) • != (Not Equal) • <, <= (Less than, less than or equal to) • >, >= (Greater than, Greater than or equal)
Standard Input • Reads input and outputs if the line was empty or not. • $line=<STDIN>;if($line eq “\n”){ print “Empty line. \n”} else { print “Not empty line. \n”}
Standard Input II • Example of Perl’s “There is more than one way to do it” • Reads a line, outputs the line, and stops at end of file. • More strict version:while(defined($line=<STDIN>)){ print “This line was: $line \n”; } • Shortcut version:while(<STDIN>){ print “This line was: $_ \n”; }
Lists and Arrays • Lists are very user friendly in Perl.The sigil of an array(list) is @ • (1,2,3,4,5,6,7) # A list of numbers. • (1..7) # The exact same list, created by the range operator. • ($a..$b) # Range set by variables. • Accessing Lists: Very similar to how you would access a C++ Vector. • @numbers=(1..5);print $numbers[0]; #Prints 1 – Lists begin at 0print $numbers[2]; #Prints 3print $numbers[5]; #Nothing is printed, value of anything out of range is undef. • Print $numbers[$#numbers]; #prints 5, the last element of the list.
Lists and Arrays II • Swaps the values of $barney and $fred • ($fred, $barney)=($barney, $fred) • Reading into a list: • While(<>){ $lines[$index]=$_; $index=$index+1;}print $#lines+1; #This will print out the number of linesprint @lines; #This will print out the list itself
Resources and Citation. • www.perl.org • http://www.cs.drexel.edu/~knowak/cs265_fall_2010/Perl_resources.htm