LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 08-28-2006, 01:59 AM   #1
opto
Member
 
Registered: Jun 2006
Location: Pennsylvania
Distribution: Slackware64 , OS X , OpenBSD
Posts: 56

Rep: Reputation: 15
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?
 
Old 08-28-2006, 02:46 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
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.
 
Old 08-28-2006, 12:28 PM   #3
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
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.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
yum, firefox, ATI drivers, bashrc, perl/Tk, Logitech keyboard, etc - lots to fix!!!! Nalorin Linux - Software 9 06-23-2005 12:45 AM
Javascript and Radio Inputs. swatward Programming 1 05-06-2005 04:51 AM
MythTV claims all my inputs are in use. None actually are Pcghost Linux - Software 2 03-24-2005 10:42 AM
No Sound from Motherboard Inputs? braincandy99 Slackware 3 07-18-2004 06:22 AM
getting info on video inputs Jo_Nak Programming 1 06-16-2003 03:40 PM


All times are GMT -5. The time now is 02:24 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration