473,378 Members | 1,319 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,378 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 8495
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

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

Similar topics

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...
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? ...
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...
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"; ...
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
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";
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...
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...
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 +=...
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...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.