471,862 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,862 software developers and data experts.

How to use <STDIN> in Active Perl for getting user input?

Hi every one, I am new in perl. I m using Active Perl 5.8.7 Window based. When i use <STDIN> for getting user input in my perl code, after executing i dont get any output on the screne. Following is simple code:
Expand|Select|Wrap|Line Numbers
  1. print "What is your name?\n";
  2. $name = <STDIN>;
  3. print "Your name is: $name\n";
PROBLEM: When I execute I dont get any output on screne. I mean, "What is your name?" is not printed on the screne. But the program remains in execution phase waiting for input. if i write some thing, say my name, on the output window (console), then the whole program is executed properly. I want "What is your name?" should be shown first so that the user may know that the program is asking for the input, as it is practiced in all other programming languages. I have not met this problem in any other language. Please help me, if anyone knows.
Jan 26 '11 #1
10 8300
Set at the beginning of your program:


Don't forget use strict; use warnings;

Think again whether it is really a good idea to interact with the user via STDIN.
Jan 26 '11 #2
589 Expert Mod 512MB
Add this prior to the print statements.

Jan 26 '11 #3
80 Expert
Strange, I don't have this problem with Strawberry Perl 5.10.1, 5.12.0 and ActivePerl 5.8.8 (all are for Windows).

Are you sure that your program has "\n" at the first line? Perl buffers all that is printed before it encounters \n.
Jan 26 '11 #4
Thanx dear,
The problem has been solved both ways: $I = 1; at beginning, and $I++ ;before the print statement.
The Use of use warnings; is okay but use of use strict; does not work and generates error
Jan 30 '11 #5
> but use of use strict; does not work and generates error

In this case, I strongly suggest fixing the errors, instead of removing strict....
Jan 30 '11 #6
Following is my code:
use strict;
use warnings;
print "What z ur age?\n";
$age = <STDIN> ;
print "Ur age is $age years\n";

And Following are the error messages:
1. Global symbol "$age" requires explicit package name at input.pl line 5.
Global symbol "$age" requires explicit package name at input.pl line 6.
Jan 31 '11 #7
80 Expert
You need to declare $age variable.
Here is my version of your program:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use IO::Handle;
  4. STDOUT->autoflush(1); #more readable than $|=1;
  5. print "What z ur age;?\n";
  6. my $age = <STDIN> ;
  7. chomp($age); #remove newline symbol at the end
  8. print "Ur age is $age years\n";
Jan 31 '11 #8
And what exactly is your question? I think the error message says it all: You are using a global variable without specifying the package. Under strict, this is forbidden (for good reason, because it global variables are a bit dangerous and it is well possible that you just forgot to declare the variable locally.

Hence, if you really want to have a global variable here, you have either to qualify it with the package name ($YOURPACKAGENAMEGOESHERE::age), or declare it with 'our' (see perldoc -f our).
Jan 31 '11 #9
Thanx a lot dear:
The problem of errors is also solved just by using "my" or "our" before the "$age" variable while declaring the variable. i could not understand "$YOURPACKAGENAMEGOESHERE::age". how to incorporate it in the code?
Thanx to all who helped me.
Feb 1 '11 #10
> The problem of errors is also solved just by using "my" or "our" before the "$age" variable while declaring the variable

Of course it is. In the first case, you declare it as a lexically scoped local variable. In the second case you declare it as a (lexically scoped) global variable. If you don't want to declare it, and you want to be it a global variable, you have to explicitly name the package in which it goes. This was the third alternative.

First of all you should make up your mind whether you want the variable be local and global. As a rule of thumb, if you are not sure that you need a global one, stick to a local variable. Hence, the declaration using "my" makes most sense, unless you have good reason doing it otherwise. Using 'strict' makes sure that you don't forget about these declarations.
Feb 1 '11 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

31 posts views Thread by da Vinci | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
1 post views Thread by jcor | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.