Download Perl Script Exercises: Understanding Strings, Numbers, and Control Structures and more Lecture notes Programming Languages in PDF only on Docsity! #!/ usr/bin/perl # Author: <fill in your name > # Perl script containing COMP284 Perl exercises. print "Hello World\n"; b. Save that the code to a file named perlExercise in some appropriate directory. c. Open a terminal, go to the directory in which the file has been stored. d. Make sure that the file perlExercise is executable by using the command chmod u+x perlExercise e. Now execute the Perl script using the command ./perlExercise and check that the output is: Hello World! 2. We also looked at single quoted and double quoted strings and the different interpretation of the backslash character in these. a. Add the following code at the end of your Perl script, save the file and then execute it. 1 docsity.com $text = "stop !"; print โdon\โt \โdon\โt\โ "don\โt" \U$text โ,"\n"; print "don โt โdon โtโ \"don\โt\" \U$text ","\n\n"; print โglass\\table glass\table glass\ntable โ,"\n"; print "glass\\table glass\table glass\ntable ","\n\n"; b. Check that the output is as you would expect. c. Add two print statements to your Perl script that produce the following output: โThere โs no fun in Java .\\" One statement should use single quoted strings the other double quoted strings. 3. In the lectures you have learned that Perl converts between strings and numbers depend- ing on the context. a. Add the following code at the end of your Perl script, save the file and then execute it. $student_id = "200846369"; $staff_id = "E00481370" ; print "student_id = $student_id; staff_id = $staff_id\n"; $student_id ++; $staff_id ++; print "student_id = $student_id; staff_id = $staff_id\n"; $student_id += 1; $staff_id += 1; print "student_id = $student_id; staff_id = $staff_id\n"; b. Try to figure out what is going on. Hint: Consult http://perldoc.perl.org/perlop.html#Auto-increment-and-Auto-decrement 4. In the lectures you have learned that Perl distinguishes between numeric comparison op- erators and string comparison operators. a. Add the following code at the end of your Perl script if (35 == 35.0) { print ("35 is numerically equal to 35.0\n") } else { print ("35 is not numerically equal to 35.0\n") } if (โ35โ eq โ35.0โ) { print("โ35โ is string equal to โ35.0โ\n") } else { print("โ35โ is not string equal to โ35.0โ\n") } if (โ35โ == โ35.0โ) { print("โ35โ is numerically equal to โ35.0โ\n") } else { print("โ35โ is not numerically equal to โ35.0โ\n") } if (35 < 35.0) { print ("35 is numerically less than 35.0\n") 2 docsity.com