input <>; not working?

B

Billy

I ran into a problem that I went round-and-round on until I broke an input
to the simplest test code I could think of and it still doesn't work...

print "Enter a number: ";
$number = <>;
print "The number is $number.\n";

or

$number = <STDIN>;
print STDOUT "The number is $number.\n";

What happens with both attempts is it prints:

Enter a number: The number is.

without pausing for an input.....

All other parts of my code works except when I want to get an input from the
keyboard....
What am I missing?

Billy
 
A

A. Sinan Unur

I ran into a problem that I went round-and-round on until I broke an
input to the simplest test code I could think of and it still doesn't
work...
....

$number = <STDIN>;
print STDOUT "The number is $number.\n";

What happens with both attempts is it prints:

Enter a number: The number is.

without pausing for an input.....

All other parts of my code works except when I want to get an input
from the keyboard....
What am I missing?

I am not sure:

use strict;
use warnings;

$| = 1;

print 'Enter a number: ';
my $number = <STDIN>;
chomp $number;
print "The number is $number.\n";

__END__

C:\Dload> t.pl
Enter a number: 5
The number is 5.

C:\Dload> perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2004, Larry Wall

Binary build 811 provided by ActiveState Corp. http://www.ActiveState.com

C:\Dload> ver

Microsoft Windows XP [Version 5.1.2600]
 
B

Billy

A. Sinan Unur said:
C:\Dload> perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Perl version 5.6.0 built for IP27-irix

Billy
 
A

ajs

Questions:

1. How do you run this? You're using <>, which will read from files you
provide on the command-line or default to STDIN. <STDIN> might be what
you meant.

2. What's your input when you run this? Are you running it from the
command line, or are you trying to run it from inside something else
(like a Web server, which will not work)?

3. You can re-write your code to tell you a bit more:

warn "STDIN is not a terminal; may not work\n" unless -t STDIN;
print "Enter a number: ";
chomp($number = <STDIN>);
printf "The number is '%s' length %d.\n", $number, length($number);
Hope that helps.
 
B

Billy

Questions:

1. How do you run this? You're using <>, which will read from files you
provide on the command-line or default to STDIN. <STDIN> might be what
you meant.

Tried it both ways...I thought they were equivelent...
2. What's your input when you run this? Are you running it from the
command line, or are you trying to run it from inside something else
(like a Web server, which will not work)?

running it from the url line of internet explorer or netscape...
i.e. domainname.com/cgi-bin/test.pl
3. You can re-write your code to tell you a bit more:

warn "STDIN is not a terminal; may not work\n" unless -t STDIN;
print "Enter a number: ";
chomp($number = <STDIN>);
printf "The number is '%s' length %d.\n", $number, length($number);
Hope that helps.

The code above didn't give anymore then before... This has me stumped....

Billy
 
B

Brian McCauley

Billy said:
running it from the url line of internet explorer or netscape...
i.e. domainname.com/cgi-bin/test.pl

Then it looks like this ia a CGI script. It is meaningless to use <> in
this context. Don't do it. What it is you were trying to achieve?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top