LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2003, 02:44 AM   #1
farhanali
LQ Newbie
 
Registered: Apr 2003
Location: Pakistan
Distribution: Ubuntu
Posts: 15

Rep: Reputation: 0
console input in perl!


hi

i am a newbie and want to know why the following code does not take input from the console in perl.

#!/usr/bin/perl -w

print "Name?";
a$= ;
print "Hello";
print $a;
print "\n";

thanks for the help in advance.

Farhan Ali
 
Old 06-11-2003, 06:36 AM   #2
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
You must read the input with <STDIN> like

$a = <STDIN>;
 
Old 06-11-2003, 01:53 PM   #3
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Yes, you could use <STDIN> for input.

Also, you used a$, when it should've been $a. But, you should avoid using $a (or $b) in general, since these are variables that have special meaning in Perl (for sorting).

A function that will come in handy is 'chomp()', which will remove the newline that is stored in your variable automatically (unless you change the input field seperator). Otherwise you might get into trouble/errors in variable context later on.

For example if you type "123" and press enter ('\n'):
after $a=<STDIN>; $a will store "123\n"
if you then do chomp($a);, $a will now store "123".

It may not seem important right now, but it's very usefull to get rid of the newline character after input.

Last edited by coolman0stress; 06-11-2003 at 01:55 PM.
 
Old 06-12-2003, 01:47 PM   #4
farhanali
LQ Newbie
 
Registered: Apr 2003
Location: Pakistan
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
thanks guys!

i got it working now...
Farhan Ali
 
Old 06-12-2003, 01:59 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
If you want to actually prompt the user you could use:
Code:
#!/usr/bin/perl

print "What is your name: ";
$name = <>;
print "Hello $name";

exit;
 
Old 06-12-2003, 02:24 PM   #6
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Lightbulb

Well david_ross , the answer is written twice already
 
Old 06-12-2003, 02:26 PM   #7
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
:P - Half asleep - sorry
 
Old 06-12-2003, 02:27 PM   #8
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Smile

No problem
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
open(/dev/console): Input/Output error fintanmcevoy General 2 11-18-2005 06:44 AM
Standard Input in Perl Program eachow Linux - General 2 11-17-2005 12:16 AM
perl input field separator Tinkster Programming 5 10-18-2004 04:08 PM
Howto disable user input on the console? jpan Programming 15 08-13-2004 01:29 AM
perl input? andox Programming 1 06-25-2003 05:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration