in reply to taking file from user

use IO::Prompt::Hooked; my $file = prompt ( message => 'Enter your filename ("A" to abort):', tries => 10, validate => sub { -e -f shift }, escape => sub { shift =~ /^A$/ && die "Aborting." }, error => sub { die "Excessive tries; Aborting." unless $_[1]; return "[$_[0]]: Invalid filename. Please re-enter.\n\n" }, ); print "\n*** You chose '$file'. ***\n";

This solution offers the user ten attempts at producing a valid filename, and then dies if one isn't obtained. The user may also escape early by entering "A" to abort, in which case this also been coded to die.

On each attempt, the input is checked to see if it exists in the current working directory, and if it's a file. If this validation passes, the filename is printed. If not, we print a message and prompt again.

IO::Prompt::Hooked is used here, but if you want to build your own logic from scratch, IO::Prompt::Tiny would be a good choice.

If this is homework then you're probably required, instead, to prompt using the <STDIN> (diamond) operator, and do all your own validation and looping logic. But at least you might be able to get some ideas from this.


Dave