shushant has asked for the wisdom of the Perl Monks concerning the following question:

hello monks

I am taking a file as an input from user , all I want is that if the user for the first time gives wrong file name which does not belong in a directory a message should be displayed , and should ask him to renter the name . How can I do it ?

One more question , how to check whether the file input by the user is present in directory or not ??

Thanks in advance

Replies are listed 'Best First'.
Re: taking file from user
by mtmcc (Hermit) on Jul 15, 2013 at 13:37 UTC
    Something like this should work:

    #!/usr/bin/perl use strict; use warnings; print STDERR "Enter filename: "; my $fileName = <STDIN>; chomp $fileName; while (!-e $fileName) { print STDERR "\n$fileName not found.\nRe-enter filename, or +q to quit: "; $fileName = <STDIN>; chomp $fileName; exit() if $fileName eq "q"; } print STDERR "$fileName exists!\n" if -e $fileName; # <-- true if the +file exists

    -Michael

    UPDATE: it'd probably be worth your while putting in a quit option too!

      If I read OP's intent correctly, the object is to accept a file upload to a server... but michaeltmccarthy's solution appears to be for the visitor/user to download an extant file from the server.

      IMPORTANT: One more observation, in case my interp is correct: OP had best find a way to sandbox the uploads and test their content for nasties. In this day (and most previous days since ARPA-Net was the latest and greatest) allowing users effective free rein to dump garbage onto one's server/host/whatever is waaaay! to dangerous to even consider.

      If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.
Re: taking file from user
by marto (Cardinal) on Jul 15, 2013 at 13:43 UTC

    "all I want is that if the user for the first time gives wrong file name which does not belong in a directory a message should be displayed"

    How do you tell if a file blongs in a direcetory or not? Likely you want to look at -X, I suspect you want to know if the file exists, is a normal file, is readable etc.

Re: taking file from user
by davido (Cardinal) on Jul 15, 2013 at 15:47 UTC

    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

Re: taking file from user
by pvaldes (Chaplain) on Jul 15, 2013 at 19:48 UTC

    > "gives wrong file name which does not belong in a directory"

    let see... what if your file is a link in another directory pointing to a correct filename in the correct directory?

    it depends on what do you understand by wrong filename. A wrong filename and a wrong file are very different things

    If you want to validate your file think instead about an "open file or die/warn/try again/plan B" line