26. How does the real perl program look like: #!/usr/local/bin/perl print “Hello everyone”; Mandatory first line (on UNIX) How to run it: 1. Save the text of your code as a file -- program.pl 2. Execute it: perl program.pl Hello everyone
27.
28. 2+2 = ? $a = 2; $b = 2; $c = $a + $b; $ - indicates a variable ; - ends every command = - assigns a value to a variable $c = 2 + 2; or $c = 2 * 2; or $c = 2 / 2; or $c = 2 ^ 4; or 2^4 <-> 2 4 =16 $c = 1.35 * 2 - 3 / (0.12 + 1); or
29. Ok, $c is 4. How do we know it? print “Hello ”; print command: $c = 4; print “$c”; “ ” - bracket output expression - print a end-of-the-line character (equivalent to pressing ‘Enter’) print “Hello everyone”; print “Hello” . ” everyone” . “”; Strings concatenation: Expressions and strings together: print “2 + 2 = “ . (2+2) . ””; expression 2 + 2 = 4
30. Loops and cycles ( for statement): # Output all the numbers from 1 to 100 for ($n=1; $n<=100; $n+=1) { print “$n ”; } 1. Initialization : for ( $n=1 ; ; ) { … } 2. Increment : for ( ; ; $n+=1 ) { … } 3. Termination (do until the criteria is satisfied) : for ( ; $n<=100 ; ) { … } 4. Body of the loop - command inside curly brackets : for ( ; ; ) { … }
31. FOR & IF -- all the even numbers from 1 to 100: for ($n=1; $n<=100; $n+=1) { if (($n % 2) == 0) { print “$n”; } } Note: $a % $b -- Modulus -- Remainder when $a is divided by $b