Question: ~ Perl Assignment 03 ~ Objectives To get practice in using Perl file handles, program arguments, and regular expressions. Create a perl script that will

~ Perl Assignment 03 ~

  • Objectives
    • To get practice in using Perl file handles, program arguments, and regular expressions.
    • Create a perl script that will read in a text file of possible phone numbers and print out those numbers that fit a regex of a phone number.
  • Perl Program 03
    • Download the text file ph_numbers.txt and ensure it is in the same folder you will be using for this assignment.
      • This text file contains valid and invalid phone numbers on separate lines.
      • Right-click on the link above and Save link as to download the file.
    • Create a perl script named LastnameFirstname03.pl.
      • Ensure that ph_numbers.txt and LastnameFirstname03.pl are in the same folder.
    • Include strict and warnings at the top of your script.
    • This script will use exactly 1 program argument.
      • The program argument will be the name of a file the script will read in.
    • Terminate the script if there is not exactly 1 program argument.
      • Output an appropriate error and usage message stating to the user what the argument should be.
      • See the error and usage messages in the Example Outputs on the bottom. You may adapt the error and usage messages for your own scripts.
    • Retrieve the file name from the program argument array and store it in a scalar variable.
    • Attempt to open a file handle for reading using the above scalar variable, or terminate the script if the file handle cannot be opened.
      • Although you are using ph_numbers.txt for the assignment, I should be able to run your script with any file name that I enter as a program argument. In other words, do not hard-code the file name "ph_numbers.txt" inside the script, it should not appear anywhere.
      • To terminate the script if the file handle cannot be opened, use a conditional or die statement with the openinstruction.
        • An example of an open-or-die statement is in the extra example from the File I/O lecture.
    • Read through each phone number in the file.
      • Each phone number is on its own line, therefore read the file line by line.
      • At this point, you may want to read through and print all the lines in the file just to make sure everything is working.
    • Create a regex pattern that will match what a phone number looks like.
      • A valid phone numbers can be in either of these formats:
        • 000-000-0000
          • 3 digits, a dash, followed by 3 digits, another dash, followed by 4 digits.
        • (000)000-0000
          • An open parenthesis, followed by 3 digits, followed by a closing parenthesis, followed by 3 digits, a dash, followed by 4 digits.
      • Since parentheses ( ) are normally used for grouping, you will need to escape each parenthesis to match it:
        • To match an opening parenthesis: \(
        • To match a closing parenthesis: \)
    • As your script is reading line by line, apply your phone number regex to test if the line looks like a phone number.
    • Print out only phone numbers that satisfy your regex and ignore phone numbers that do not match your regex pattern.
    • See Example Output for the expected output of your script.
      • There are only 6 valid phone numbers, the rest are invalid.
    • Ensure that your code is sufficiently styled and documented.
      • Styled meaning code is indented when applicable, variables are named properly, etc.
      • Documentation includes a program description at the top and in-line comments.
      • See posted examples in the Lecture Material page.
  • Extra Credit (up to 2 points)
    • +1 Instead of printing the valid numbers, write them out to a text file named valid_ph_numbers.txt on separate lines.
      • Overwrite valid_ph_numbers.txt each time the script is run, i.e. do not append.
    • +1 Complete the above extra credit and print out the invalid phone numbers, see Example Output below
  • Example Outputs
    > perl firstlastname.pl Error: Expecting 1 program argument. Found 0 instead usage: perl firstlastname.pl filename > perl firstlastname.pl pew Unable to open file: pew  > perl firstlastname.pl 1 2 3 4 Error: Expecting 1 program argument. Found 4 instead usage: perl MeyerEdward03.pl filename > perl firstlastname.pl ph_numbers Unable to open file: ph_numbers > perl firstlastname.pl ph_numbers.txt 415-555-1234 650-555-2345 (416)555-3456 (123)456-7890 (808)234-5678 808-234-5678
  • Extra Credit Example Output +2
    > perl MeyerEdward03.pl ph_numbers.txt Invalid: 1234567890 Invalid: r415-555-1234 Invalid: 5-1-9 Invalid: (123)4e6-7890 Invalid: 123)456-7890 Invalid: (123)-456-7890 Invalid: 123-456-7890a Invalid: 808-5555-1234 Invalid: (123456-7890 Invalid: 123-1234 Invalid: 650-5_5-2345 Invalid: 808(234)-5678 Invalid: zz8808-555-1234 Invalid: qqqqq(808)555-090900 Invalid: 808 234 5678 Invalid: 808123-5678 Done! Valid numbers written to valid_ph_numbers.txt
  • Hints
    • My solution without any comments was ~21 lines of code - exact number of lines may differ depending on your own approach.
    • My extra credit solution without any comments was 25 lines of code.
    • Use regex101.com to test your regex pattern. Paste the contents of ph_numbers.txt in the test string area. Note that regex101.com has the g and m flags on by default, but you will not be using those flags when you implement the pattern in your script.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!