Try using Term::ReadKey you can make the shell behave anyway you want by setting the ReadMode. Here is some code I wrote to capture a key, and echo back a * on a Win32 machine. It did not behave properly until (tye) got me to try putting STDIN in binmode. This may be over-board for you... play with it :)
#!/perl/bin/perl use Term::ReadKey; while(1){ ReadMode 3; #cbreak mode &menu; print "\tChoice: "; my $key = ReadKey(); print "$key"; &hello_world if $key == 1; &get_password if $key == 2; if( $key eq 'q' || $key eq 'Q'){ print "\nExiting"; exit(0); } } sub get_password{ binmode STDIN, ":raw"; ReadMode 3; my ($key, $val, $pass, $backstop); my @password; my $prompt = "\n\n\tPassword: "; system("cls"); # NOTE if you make a call to the system you have to +reset the ReadMode :) #After we make the system call... ReadMode is set back to normal or 0! +! ReadMode 3; print "$prompt"; while($key = ReadKey()){ $val = ord $key; push @password, $key unless $val == 8 || $val == 13; $backstop = @password; last if $val == 13; if($val == 8){ pop @password; if($backstop) {print "\b \b"; } } print "*" unless $val == 8; } ReadMode 0; binmode STDIN, ":crlf"; foreach(@password) { $pass .= $_; } print "\n\n\tYou entered: $pass\n\t(will return to main menu in 3 tick +s)"; sleep 3; system("cls"); } sub menu{ print <<MENU; [1] Hello World [2] Password Sub [Q] Exit MENU } sub hello_world{ system ("cls"); for(1..10){ print "\n\t\tHELLO WORLD \n"; } print "\n\t(will return to main menu in 3 ticks)"; sleep 3; system ("cls"); }
Update: 13 is the ord value of the enter key and 8 is the backspace, you will need to use 10 for your enter key on a unix OS. Windows consoles "steal" the delete and arrow keys so I can't catch them. You may be able to add them to handle rubouts, it just wouldn't do it on Win32, most folks use backspace to rubout so I didn't try too hard to find a way to bind to the delete key.

In reply to Re: ~ simple command line program ~ by JamesNC
in thread simple command line program by primus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.