1 / 16

Perl Basics – Part I

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.

wpenny
Download Presentation

Perl Basics – Part I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. print 'Hello world.'; Perl Basics – Part I Tren Griffith

  2. Outline: • Perl introduction • Scalar Data • Variables • Operators • Control Structures • Input • Lists and Arrays • Comparison Examples

  3. 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.

  4. 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

  5. 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

  6. 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”

  7. 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.

  8. Control Structure • If Statement: • if(…){…} • If Else Statement: • if(…){…} else { • …} • While Statement • while(…){…}

  9. 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.

  10. 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)

  11. 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”}

  12. 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”; }

  13. 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.

  14. 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

  15. Resources and Citation. • www.perl.org • http://www.cs.drexel.edu/~knowak/cs265_fall_2010/Perl_resources.htm

  16. The end. Any questions?

More Related