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

How do I get user input? I tried using <STDIN> in EditPlus, and it crashes every time! I am new to Perl (started yesterday) and want to get going. But I cant do squat if I dont get user input! Help me, and anything is greatly appreciated.

20031211 Edit by Corion: Changed title from Help! Please Help...

Replies are listed 'Best First'.
Re: Getting user input
by davido (Cardinal) on Dec 10, 2003 at 23:13 UTC
    I'm not familiar with how EditPlus relates to taking input in a Perl script.

    The following script will prompt for and take user input via STDIN, place it in a variable named $input, strip away the trailing newline, and will print what the user typed in, to STDOUT.

    #!/usr/bin/perl use strict; use warnings; print "Enter your input:\n"; my $input = <STDIN>; chomp $input; print "You typed: $input\n";


    Dave

Re: Getting user input
by Abigail-II (Bishop) on Dec 10, 2003 at 23:56 UTC
    How do I get user input? I tried using <STDIN> in EditPlus, and it crashes every time!
    That's not very helpful. Whatever EditPlus is, it's not part of the Perl distribution, so that's hardly relevant. What you need to do is 1) show what you did, 2) tell what you expect it to do and 3) tell what it did. That will maximize your chances in getting a useful answer.

    Abigail

Re: Getting user input
by young_david (Friar) on Dec 11, 2003 at 03:22 UTC

    If I'm not mistaken EditPlus runs on Windows only, but I don't know how stable it is. So, you may want to run your scripts from a DOS prompt instead of EditPlus.

    Check out the Tutorials section. Read up on "How to Ask Questions" and "Getting Started with Perl".

    Also, you definitely want to pick up a copy of "Learning Perl 3rd Edition". It's a must for getting started.

Re: Getting user input
by b10m (Vicar) on Dec 10, 2003 at 23:38 UTC
    You might want to take a look at Term::ReadKey. I use that and it works like a charm for me.
    --
    B10m
Re: Getting user input
by bart (Canon) on Dec 11, 2003 at 12:51 UTC
    I tried using <STDIN> in EditPlus, and it crashes every time!
    EditPlus is a text editor, right? And you're trying to run perl as a tool, executing your script in a hidden manner, and capturing its output into an editor window? I don't know what terminology EditPlus uses, but if I'm right, you should know what I mean: it's as if running a compiler from within the editor.

    Well, I guess that Perl doesn't actually crash, but that it hangs — waiting for input, which it can't get because you can't enter it.

    I can't think of a quick solution right now, except for not doing it this way, and instead, run the script from the command line (also known as the "DOS prompt"). For non-interactive scripts, running a script from within the editor is just fine.

    Getting user input in another manner, via a GUI, is another, neat solution, however, it requires a lot of work, and I don't think a module for doing an "inputbox" (like a messagebox with a text widget — as available in VBscript, and Javascript too, IIRC) is available. Yet. You probably wouldn't want to use it anyway because you won't install modules </sneer>

      You can use TextPad on windows and make the perl interpreter a menu command. Then it executes the currently open file from a dos window. Textpad will also use wperl if you map that command too. It's quite easy. If you need help just pm me.

      p.s. it maps the command to CTRL\d ..for instance my perl interpreter is mapped to CTRL+1

      Grygonos
Re: Getting user input
by Anonymous Monk on Dec 11, 2003 at 16:21 UTC
    Thanks folks. I appreciate all your comments, and they helped. As for EditPlus....BAH! It shall be removed from my box post haste. Thanks again, Senior PartyMarty (ole!)