Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-28-2006, 01:59 AM   #1
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/./ line 7.

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

Global symbol "$answer"requires explicit package name at/home/me/projects/sweather/./ 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
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

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

#!/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.


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
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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:06 PM.

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