Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Help with getting the ouput from the script

by freekngeek (Acolyte)
on Jun 03, 2013 at 13:15 UTC ( #1036745=perlquestion: print w/replies, xml ) Need Help??
freekngeek has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everyone, I have a perl script and what I want is to run it in a terminal and I want it to give me two options :

yes or No

If I write "yes" into the terminal then perl scipt should give me the output. and if i say "no" then it should exit from the terminal.

I hope someone could help, I am new to perl

  • Comment on Help with getting the ouput from the script

Replies are listed 'Best First'.
Re: Help with getting the ouput from the script
by davido (Archbishop) on Jun 03, 2013 at 14:54 UTC

    use IO::Prompt::Hooked; my $result = prompt ( message => 'Shall we continue? (yes/no)', default => 'yes', validate => qr/^(?:yes|no)$/i, error => "The only two options are 'yes' or 'no'. Try again.\n", ); if ( $result =~ m/^no$/i ) { print "Ok, we're done.\n"; exit( 0 ); } print "Great... let's continue...\n";

    I'm wondering, if you're having trouble taking user input and branching your logic, who wrote the part of your script that's working great?


Re: Help with getting the ouput form the script
by hippo (Abbot) on Jun 03, 2013 at 13:38 UTC

    Which part of this task are you stuck on? Can you read the user's input? Can you store it? Can you compare it to your desired inputs? Can you perform a logical branch on the basis of the comparison?

    What have you tried?

      I have a perl script which is parsing some text from some text file and creating a new file in which all the values are in tabulated form. Script is running fine and smooth

      Now, What I want is to interrupt(I dont know if it's a right word) script in terminal, So i can have two options like Yes or No.

      <I am stuck at this part. I don't know how to make my script behaves like this.

        Let's assume therefore that your problem is that you do not know how to perform a read from standard input. Take a look at this:

        my $a = <>;

        Do you know what this does? Are you familiar with the diamond operator?

Re: Help with getting the ouput form the script
by Anonymous Monk on Jun 03, 2013 at 13:36 UTC
Re: Help with getting the ouput from the script
by greengaroo (Hermit) on Jun 03, 2013 at 17:51 UTC

    Fellow monks, please, for a simple question, give a simple answer.

    Monk freekngeek, what you want to do is to read from the command-line from your script. This is part of the input/output chapter. The output part, you probably know already. When you use the print function, you use the output.

    To use the input and output in Perl, you need what is called "FileHandles". The print function uses a default FileHandle named "STDOUT" (Standard Output).

    You want to read from the STDIN (Standard Input). How to do that? Try this:

    my $a = <STDIN>;

    It will give the same result as Monk hippo mentioned above. In his example, the STDIN FileHandle was implied, the same way the STDOUT FileHandle is implied when you use the print function:

    print "Hello world";

    Is the same as:

    print STDOUT "Hello world";

    Of course, you can use CPAN modules to achieve reading from the command-line, like Monk davido mentioned above, but I suggest before you venture in that direction, that you learn the basics of Input/Output in Perl. Search for "FileHandle" and "STDIN" and you shall find your answers.

    A for will get you from A to Z; a while will get you everywhere.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1036745]
Approved by Corion
[jedikaiti]: Hanging in there, Discipulus - you?
[Discipulus]: ah fine! released my first Dancer website, and today it's a special day
[Discipulus]: my son finished 18 today

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2018-01-18 19:52 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (214 votes). Check out past polls.