1 / 24

Perl Lecture #1

Perl Lecture #1. Scripting Languages Fall 2004. Perl. Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s needed a quick language didn’t want to resort to C derivative of sed or awk (interpreted language used on Unix / sed – stream editor. Perl Intro Cont’d.

vera
Download Presentation

Perl Lecture #1

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. Perl Lecture #1 Scripting Languages Fall 2004

  2. Perl • Practical Extraction and Report Language • -created by Larry Wall -- mid – 1980’s • needed a quick language • didn’t want to resort to C • derivative of sed or awk (interpreted language used on Unix / sed – stream editor.

  3. Perl Intro Cont’d • fills the gap between C and awk • very powerful language / easy to learn • used to write small scripting programs as well as larger applications • for the web has been used –cgi scripts – run forms etc. • also web apps – shopping cart applications • makes use of regular expressions – powerful sequence of characters

  4. Why Use? • Perl is free • many Perl ide’s are free – works great integrated in a Unix environment as most version come with Perl / Mod-Perl and Emb Perl • Works well in a Windows environment as well • CPAN – comprehensive Perl Archive Network

  5. #!/usr/bin/perl #Author: Lori N #Description: First program #Date: Today’s Date $string="Top 10"; $number=10.0; print "Number is 10.0 and string is 'Top 10'\n\n"; $add = $number + $string; print "Adding a number and a string: $add\n"; $concatenate = $number . $string; print "Concatenating a number and a string: $concatenate \n"; $add2 = $concatenate + $add; print "Adding the previous two results: $add2 \n\n"; $undefAdd = 10 + $undefNumber; print "Adding 10 to an undefined variable: $undefAdd\n"; print "Printing an undefined variable: $undefVariable(end)\n";

  6. $Scalar • When we have just one of something we have a scalar • simplest kind of data that Perl Manipulates. • either a number or a string of characters • Perl uses them interchangeably • no need to declare a variable • Perl will figure it out by its usage

  7. Numbers • int and floating pt numbers • Perl computes with double-precision fp values • Literal • is not a result of calculation or I/O op – data written directly into the source code • 0 • 2001 • -4 • also use Octal ( base 8 ) , hexadecimal ( base 16)

  8. Strings • seq of characters • they have a literal representation – ‘single quoted’ and “double quoted” • Single quoted Literals - ‘string’ - ‘string\’s’ - ‘hello\n’ – no newline

  9. Double Quoted Strings • Double quoted Literals --“string” --“string\n” – newline --“string \””

  10. String operators: • “hello” . “world” = helloworld • “hello” . ‘ ‘ . “world” = hello world String repetition operator • x – takes its left operand ( a string ) and makes as many concatenated copies as you specify • “string” x 3 -- stringstringstring • 5 x 4 -- 5555

  11. Automatic Conversions • Perl automatically performs conversions between Numbers and Strings. • by the operator used or they way you attempt to use them in your script -- be careful this might not work out logically like you’d like it to

  12. Warnings • Perl’s Built in Warnings. • Command line – perl –w myfile.pl • Or add it to your code #!/usr/bin/perl –w • use man perldiag to see more useful troubleshooting flags • also see man perllexwarn man page for warnings that can be turned on and off.

  13. Scalar Variables • variable – all should be familiar – they hold values • a scalar variable holds a single scalar value • they all begin with $Perl_identifier • can’t start with a digit • they are also referenced with the leading $

  14. Scalar Assignment • --assignment  • --$income = ‘tolittle’; • --$tax_amount = 1000; • --$miles = 100; • --$distance = $miles * 5; • Similar binary operators as C • --+= , *= , .= (string concatenator)

  15. Output • --print “Hello World\n”; • --print ( ) • --in a series separated by comma’s • --print “My income is “, 0 * 10000 , “.or null\n”;

  16. Interpolation of Scalar variables into Strings • $income = “not much”; • $expenses = “quite a bit”; • $lifesavings = “My income is $income but my expenditures are $expenses”; • or $lifesavings = ‘My income is ‘ . $income . ‘but my expenditures are ‘ . $expenses; • Book has table on page 32 – Operator Precedence and Associativity

  17. Comparison Operators • < <= == >= > != • Strings – eq , ne , lt , gt , le , ge

  18. if Control Structure • if ( $variable <= $anothervariable) { • Print this; • } • Curly braces are required

  19. No Boolean Data Type • No Boolean data type – used simple rules: • --the undef value • --what if you use a scalar value before you give it a value? • --Perl gives it a undef value – neither a string or a number • --acts like zero – or an empty string • --for Boolean process uses simple rules • --undef is false • --Zero is false – all else true • --empty string ‘ ‘ is false – all else true • --The one exception – since numbers and strings are equivalent, the string form of zero, ‘0’ has the same value as its numeric form – false

  20. User Input • --line-input operator <STDIN> • --Perl reads the next complete line of text from standard input ( up to the first newline) • --uses it as the value of <STDIN> • --its string value has a newline character on the end of it : • $line = <STDIN> • if ($line eq “\n”) { • print “That was just a blank line!\n”; • }else { • print “That line of input was : $line”; • }

  21. Chomp Operator • --works on a variable • --variable has to hold a string • --if the string ends in a newline – it removes it • --take input from <STDIN> -- chomp removes \n and • provides us with just the string. • One step: • chomp($variable = <STDIN>) • --chomps return value is the number of characters removed – 1

  22. while Control Structure • --same as C++

  23. Tutorial:Simple Perl program: #!/usr/bin/perl –w # #Name: Add Name #Date: Today’s Date #Description: first.pl Ask and Display name print “Please enter you name “; $name = <STDIN>; chomp ($name); print “Your name is $name”;

  24. Execute Your Code • Has to be executable: • chmod 755 first.pl • To run two ways: • perl first.pl • or ./first.pl

More Related