473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 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";
  4.  
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 8490
rovf
41
Set at the beginning of your program:

$|=1;


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
RonB
589 Expert Mod 512MB
Add this prior to the print statements.

$|++;
Jan 26 '11 #3
chorny
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
rovf
41
> 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;
$|=1;
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
chorny
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";
  9.  
Jan 31 '11 #8
rovf
41
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
rovf
41
> 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

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

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
4
by: jonniethecodeprince | last post by:
#This is a program from dummies.com $TheDB = 'edata.txt'; # Open the database file but quit if it doesn't exist open(INDB, $TheDB) or die "The database $TheDB could " . "not be found.\n"; ...
2
by: jx2 | last post by:
i need something like PERL $in = <STDIN>; is there any way to do it in PHP CLI ? thanks a lot jx2
1
by: jcor | last post by:
Hi, I want to read some stuff from the input line and build a string with it. My code is: print "insert title\n"; $title=<STDIN>; $output="_"."$title"."_1000"; print "$output\n";
3
by: jcor | last post by:
Hi, I'd like to know how I cancount characters user inputs while they input it. Imagine something like this: See this example how this could work: input your title (2 chars): My input your...
0
by: friendofgod | last post by:
Server is Win 2003, with IIS 6, patched up to date with Windows Update, and using perl version 5.008008. Re: Page with STDIN hangs, and does a continuous load, when called in browser. Page...
1
by: Manikank | last post by:
I have a multiple lines file that I read. It reads all lines except for the last line if (defined($$bb = <STDIN>)) { print LOG" in if condition\r\n"; $readCount +=...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.