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

Hi I wanna make a program in which the user can select a file from cmd prompt. Like for example: print"Plz enter file"; now I want the user to be able to use commands like CD. CD.. etc etc to select a file. I wanna stor the result in a string. Maybe it is straight forward but please let me know how to do it. Thanks

Replies are listed 'Best First'.
Re: User Select File Capability
by ww (Archbishop) on Feb 09, 2010 at 03:34 UTC

    So let's think this through.

    1. "select a file from the command prompt"
      I'm not familiar with any form of "command prompt" -- familiarity being limited to RT-11, cpm, windoze & *nix -- that allows anyone to "select" anything. So I guess that means I'm not really going to be able to satisfy the "command prompt" part of your spec... or else that "command prompt" means something I don't understand... or else that "command prompt" means something other than what you appear to believe.
    2. "user to be able to use commands like CD"
      Well, you could spawn a new CLI with backticks (neither exec nor system will work for what appears to be your purpose) and let the user play, but that's not really a terribly secure idea, even if you're writing a program for installation on the user's own machine. So, 'fergeddaboudit'!
    3. "wanna stor the result in a string"
      Yeah, that would be really cool... but if you want to do that, you probably better build a webpage, a couple scripts to (1) deal with the form you're going to put in the html and (2) to do the work to achieve whatever your ultimate (and un-articulated) goal may be
      ...or
      step onto the learning curve for building a gui (It may be worthwhile to read perldoc Tk).

    Bottom lines:

Re: User Select File Capability
by desemondo (Hermit) on Feb 09, 2010 at 02:08 UTC
    What have you tried, where is your code that you wrote and got stuck?

    What you mean CD? Do you want them to be able to navigate/browse around a directory? If so, do you really need that? Or would it be simpler to just use cmd line arguments, or one of the Getopt modules?

    As for the rest of your question, the below seems to cover it, based on my understanding of your question. If that's not it, maybe you should re-read How do I post a question effectively? and clarify your question...
    use strict; use warnings; print "Plz enter file\n"; my $file = <STDIN>;

      You might want to chomp the filename to get rid of the line terminator so as to avoid headaches with "file not found" errors.

      use strict; use warnings; use 5.010; my $prompt = q{Plz enter file: }; print $prompt; my $file1 = <STDIN>; say for map sprintf( q{%#04x - >%s<}, ord, $_ ), split m{}, $file1; print $prompt; chomp( my $file2 = <STDIN> ); say for map sprintf( q{%#04x - >%s<}, ord, $_ ), split m{}, $file2;

      Running it produces:

      $ ./spw822114 Plz enter file: xyz.c 0x78 - >x< 0x79 - >y< 0x7a - >z< 0x2e - >.< 0x63 - >c< 0x0a - > < Plz enter file: xyz.c 0x78 - >x< 0x79 - >y< 0x7a - >z< 0x2e - >.< 0x63 - >c< $

      I hope this is of interest.

      Cheers,

      JohnGG

Re: User Select File Capability
by Utilitarian (Vicar) on Feb 09, 2010 at 10:58 UTC
    So you need to create a menu of the files in the current directory with the option to change directory to any sub-directory in the current directory or go up a level and the possibility of selecting a file in the current directory.

    Something like Norton commander of old? or Midnight commander (mc) on Linux?

    Break your problem into manageable bits.

    • Write a menu function which takes an array, displays the contents with an identifier and returns the value associated with the identifier the user chooses
    • Use readdir on the current directory to call the menu sub above with the contents of the curent directory
    • If the returned value is a directory cd to it and repeat the process
    • until the returned value is a file, in which case select it for processing.
    • Process file.
    Each of the steps above is straightforward, try coding them up and, if you have a problem come back to us with your code, the inputs and the errors you are seeing.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."