PERL Assignment 1 This assignment will give you exposure to basic scripting concepts and PERL. The purpose of the assignment will be to create a series of scripts, some of which will be used next week, to perform basic tasks. When you are finished the assignment, you should have the following files in your Perl directory: helloworld.pl argworld.pl template.pl power.pl vis5d.pl HAND IN: helloworld.pl argworld.pl template.pl power.pl vis5d.pl These files will be collected for marking on 11:59 pm March 11, 2011. Make sure your final copies are in your Perl directory! ============================================================================= PART 1: HELLOWORLD.PL First we'll get some experience setting up a basic script and doing a simple print to screen. 1. Create/open helloworld.pl with your favorite editor. 2. Put the perl interpreter on the first line. 3. Add comments with your name and student number. 4. Add a line to print "Hello World!" to the screen. Also print a newline to the screen (either add to the current print statement or do it in another print statement). 5. Save the file. 6. Test it to make sure it prints the correct information to the screen. ============================================================================= PART 2: ARGWORLD.PL In this portion, we'll take the basic ideas from part 1 and expand upon them by echoing arguments passed on the command line. 1. Copy helloworld.pl to a new file called argworld.pl. 2. Take out the print statements from argworld.pl. 3. The command line arguments are stored in @ARGV (where $ARGV[0] is the first argument). Normally we would transfer the arguments to variables for specific use later, but in this case we just want to echo all the arguments in a print statement so we can use the @ARGV array directly. Probably the easiest way to do what we want is to cycle over the elements of @ARGV using a loop and printing each element out followed by a space but no newline. 4. Once all the elements are printed, make sure a newline is printed. 5. Save the file. 6. Test the file by running it with a variety of additional arguments on the command line (ie perl argworld.pl This should print out!) Make sure your script echoes everything from the command line into a single line of output. ============================================================================= PART 3: TEMPLATE.PL In this section, we are going to setup a basic template that you can use for making future scripts. 1. Create/open template.pl in your favorite editor. 2. Put the perl interpreter on the first line. 3. Add comments at the top with your name and student number. 4. Add a comments section for describing the purpose of the script (this can be a series of blank commented lines for now). 5. Add a line to ensure 'strict' lexical scope. 6. Add a 'lib' line that locates your PERL directory. 7. Add a section for declaring variables (you can simply comment this for now). 8. Add a section for retrieving arguments. Make it check that it has the correct number of arguments (you can do this with a set of declared variables checking for a minimum and maximum number of arguments). If you have not received the correct number of arguments, use the 'die' function to print out the calling protocol. You can have another declared variable that can be set with this line. After this, have a commented section where arguments will be retrieved. Do not forget if you do add some declared variables to comment them to explain what they are for. Also, if you use the standard shortcut to get the array size, do not forget it will be 1 smaller then you might expect. 9. Add a section for the main code (currently commented). 10. Add '# EOF' to mark the end of the script. 11. Save your template.pl file. There is no need to test the file. ============================================================================= PART 4: POWER.PL In this section, you are going to write a script to calculate x^y. You will need to get x and y as arguments (handling any situation where the script is called with the wrong number of arguments), calculate x^y, then print the result to the screen. 1. Copy template.pl to power.pl. 2. Fill in the comment section describing what this script is to do and any command line arguments it expects. 3. Declare any variables you might need (such as x and y). Make sure you update the variables you added already for handling the correct numbers of arguments (you should get 2). Also setup the string for the calling protocol in case you get the wrong number of arguments. 4. Fill in the section that pulls the arguments from @ARGV. 5. Go to the main code section and add code for calculating x^y. Looping is probably the easiest way to do this. Make sure to comment your code. 6. Print the result to the screen. Something like "5^3 = 125", assuming the command line arguments were 5 and 3. 7. Save your file. 8. Test to make sure your script returns correct values and handles incorrect numbers of arguments. Do not forget to comment your code. ============================================================================= PART 5: HANDLING VIS5D TYPES In this section, you are going to create a script that takes a Vis5D type and prints out a string that describes the type. The list of types and their description strings are: ATMP "Atmospheric Pressure" FRZL "Freezing Level" GSPD "Gust Speed" HP "Hourly Precipitation" RH "Relative Humidity" SR "Solar Radiation" TEMP "Air Temperature" WCHL "Wind Chill" WDIR "Wind Direction" WSPD "Wind Speed" 1. Copy template.pl to vis5d.pl. 2. Fill in the comment section describing what this script is to do and any command line arguments it expects. 3. Declare any variables you might need. To handle the strings, you should use a hash. You can pre-populate it when you declare it, or you can add the entries later in the code section. Do not use an array or other set of variables for handling the strings. Make sure you update the variables you added already for handling the correct numbers of arguments (you should get 1). Also setup the string for the calling protocol in case you get the wrong number of arguments. 4. Fill in the section that pulls the argument from @ARGV. 5. Go to the main code section. If you did not already populate your hash, you should do that here. Use the command line argument as a key to get the description string from your hash. Print the description string to the screen. Something like "TEMP -> Air Temperature" will do (assuming TEMP was supplied). If the supplied Vis5d type is not a valid type (not in the list above), print "XXXX IS NOT A VALID TYPE!" to the screen (where XXXX is the type supplied). 6. Save your code and test it. Make sure it handles the correct number of arguments, invalid types, and prints out the correct description string for every type. Do not forget to comment your code. ============================================================================= PART 6: MAKE SURE YOUR FINISHED FILES ARE IN YOUR Perl DIRECTORY! DEADLINE: 11:59 pm March 11, 2011