LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Keyboard Inputs (http://www.linuxquestions.org/questions/programming-9/perl-keyboard-inputs-477970/)

opto 08-28-2006 01:59 AM

Perl Keyboard Inputs
 
I'm messing around with Perl (fro the first time) and I'm trying to figure how to let the user input with the keyboard. This is part what I have so far:


#!/usr/bin/perl -w
#
use strict;
use warnings;
#program version
my $VERSION="0";
$answer = <STDIN>;#Line 7
chomp ($answer = <>);
print "$answer";


The errors that I get are:

Global symbol "$answer"requires explicit package name at/home/me/projects/sweather/./sweather.pl line 7.

Global symbol "$answer"requires explicit package name at /home/me/projects/sweather/./sweather.pl line 8.

Global symbol "$answer"requires explicit package name at/home/me/projects/sweather/./sweather.pl line 9.

Keyboard inputs are usually pretty easy to do in other languages. I was folloowing a tutorial on the Internet. Does anybody have any suggestions?

chrism01 08-28-2006 02:46 AM

If you do
use strict;
which is a good idea, you need to declare vars before using them, like C.
I can recommend the Llama book (Learning Perl) if you haven't already got it.
Here's the lang pages, inc examples: http://perldoc.perl.org/
You can use them instead of buying Programming Perl, but I highly recommend Perl Cookbook if you are serious about learning Perl; lots of pre-worked examples with explanations.

cramer 08-28-2006 12:28 PM

I made a few changes so that it works now. First of all I noticed you never seemed to prompt the user for a value for $answer but were trying to read stdin anyways. I will highlight the parts I have added / changed.

Code:

#!/usr/bin/perl -w
#
use strict;
use warnings;
#program version
my $answer; #declare $answer before using
my $VERSION="0";
print "Prompt for answer variable HERER!:  "; #prompt the user to enter something for $answer
$answer = <STDIN>; #Line 7
chomp ($answer);#chomp needs only the variable name
print "$answer";
print "\n"; #prints newline for ease of reading.



All times are GMT -5. The time now is 09:08 PM.