Question

Perl Write a program with a subroutine that keeps prompting for a filename until a valid...

Perl

Write a program with a subroutine that keeps prompting for a filename until a valid file is entered by the user (print out "file found, thanks") or until five attempts have failed (print out "you did not enter a valid file name"). The sub in this case does not return any value.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#### Perl Subroutine For getting a filename and checking whether it is a valid filename ####

sub FileNamePrompt{
$attemptsRemaining = 5; ### number of attempts
$flag = 0; ###### flag which tells whether a valid filename is entered
$fileName = "validFileName.txt"; #### a vlid filename which will compared with entered filename
  
while($attemptsRemaining > 0)
{
$attemptsRemaining = $attemptsRemaining - 1;
$input = <STDIN>; ## taking filename from input
chomp $input; ### clearing extra spaces
if($input eq $fileName) ### comparing with standard valid filename and file is found
{
print "File Found, Thanks";   
$flag =1;
last;
}
}

if($flag == 0) ### if file not found
{
print "you did not enter a valid file name";
}
}

FileNamePrompt() ### calling the subroutine

Add a comment
Know the answer?
Add Answer to:
Perl Write a program with a subroutine that keeps prompting for a filename until a valid...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Perl Program: Write a program with a subroutine to prompt the user for any message...

    Using Perl Program: Write a program with a subroutine to prompt the user for any message (like "what is your name?"). The sub will collect the user's answer and return it back to the call (in the main program above the subroutine). The sub does not accept any arguments. Print out the returned string (name) from the sub.

  • In Python, write a function numLetters() that keeps prompting the user for words until they hit...

    In Python, write a function numLetters() that keeps prompting the user for words until they hit return (using while loops). The function should then return the percent of 3-letter words that were entered. So, if a user entered ten words and 4 of the words had 3 letters, then the percent of 3-letter words is 40

  • Write a Python program that keeps reading in names from the user, until the user enters...

    Write a Python program that keeps reading in names from the user, until the user enters 0. Once the user enters 0, you should print out all the information that was entered by the user. Use this case to test your program: Input: John Marcel Daisy Samantha Nelson Deborah 0 ================ Output: John Marcel Daisy 25 Samantha Nelson Deborah Hints: Create an array Names to hold the input names. Use the Names.append(x) function to add a new name to the...

  • write the c++ code that keeps prompting (10 times) the user to enter an integer. the...

    write the c++ code that keeps prompting (10 times) the user to enter an integer. the code print out the product of all the the integers.

  • this is for script for terminal or putty question 1------ 1. Write a PERL script/program based...

    this is for script for terminal or putty question 1------ 1. Write a PERL script/program based on the following requirements. The program should to prompt the user for how many courses the plan to take next semester and ask the user to enter each of those courses before displaying all the information entered back on the screen. question 2------ 2. Write a PERL script that defines/declares an array or a list of elements. The array should hold three-letter abbreviations for...

  • Exercise 3.1 (word count.py). Write a program that takes in a filename and string as input....

    Exercise 3.1 (word count.py). Write a program that takes in a filename and string as input. Then print how many times that string appears inside the chosen file. If the file does not exist, continue asking for a filename until one is given that exists. Use your source code file as test input. Make sure to test files with that contain the same word multiple times. ? $ python3 word-count.py 2 Please enter a filename: wordcount.py 3 Please enter a...

  • Using Python, write a program that keeps asking the user for text until they type "exit"...

    Using Python, write a program that keeps asking the user for text until they type "exit" To Exit Or Not Write a program that keeps asking the user for text until they type "exit" Console 1 Enter number 1 3 Enter number 5 5 Enter number 7 Enter number 8 1 9 Enter number 10 9 11 Enter number 12 3 13 Enter number 14 4 15 Enter number 16 exit 17 Bye! 18

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

  • Q1 (2 pts) Shopping list Write a program that prompts a user for items on their...

    Q1 (2 pts) Shopping list Write a program that prompts a user for items on their shopping list and keeps prompting the user until they enter "Done" (make sure your program can stop even if the user enters "Done" with different cases, like "done"), then prints out the items in the list, line by line, and prints the total number of items on the shopping list. Output (after collecting items in the while loop) should look something like this: Apple...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT