problem when using <STDIN>

P

Paul

hi, there,

I met a wired problem when using <STDIN>, here is my script,

use Mail::pOP3Client;
use strict;
use warnings;

my $pop;
my $username;
my $password;
my $hostname;
my $SSLFag;

print "Input your username:\n";
$username=<STDIN>;
print "Input your password:\n";
$password=<STDIN>;
print "Input your hostn:\n";
$hostname=<STDIN>;
print "Input your username:\n";
$username=<STDIN>;
print "Using SSL? 1 or 0:\n";
$SSLFlag=<STDIN>;

$pop = Mail::pOP3Client->new(USER => $usernane',
PASSWORD => $password,
HOST => $hostname,
USESSL => $SSLFlag);

#$pop = Mail::pOP3Client->new(USER => 'myname',
# PASSWORD => 'password',
# HOST => 'mail host name',
# USESSL => 1);
if(!$pop)
{
print "Canot log in the pop3 server\n";
die ;
}

The problem is that if I use <STDIN> to get all four parameters and
pass them to the new function, this script will not work and no error
report. If I direct use the parameters like in the commented scripts,
it works very well.

Can you guys answer me?

Paul
 
G

Gunnar Hjalmarsson

Paul said:
I met a wired problem when using <STDIN>, here is my script,

my $pop;
my $username;
my $password;
my $hostname;
my $SSLFag;

You don't need that section with variable declarations, but you'd better
declare respective variable the first time it's used.
print "Input your username:\n";
$username=<STDIN>;

my $username = <STDIN>;
chomp $username;

or written directly:

chomp( my $username = <STDIN> );
 
S

Stephane Zuckerman

use Mail::pOP3Client;
use strict;
use warnings;

my $pop;
my $username;
my $password;
my $hostname;
my $SSLFag;

print "Input your username:\n";
$username=<STDIN>;
I'd say that your script doesn't like the final '\n' when you ask for
input (I may be wrong, however). Try using chomp :
 
A

A. Sinan Unur

print "Input your username:\n";
$username=<STDIN>;

As Gunnar pointed out, you need to chomp the input obtained, because it
contains the platform specific end of line character sequence by the
time it is stored in $username.

Also, do declare variables in the smallest applicable scope.
print "Input your password:\n";
$password=<STDIN>;

I have been using:

use Term::ReadKey;

sub prompt_password {
print 'Password: ';
ReadMode 2;
my $pass = ReadLine 0;
ReadMode 0;
chomp $pass;
return $pass;
}

for the purpose of asking for passwords. With a straightforward read
from STDIN, I found that the password was being stored in the history
buffer of cmd.exe (probably applies to other shells, too.)
The problem is that if I use <STDIN> to get all four parameters and
pass them to the new function, this script will not work and no error
report.

Well, you can diagnose issues like this by taking a look at the variable
to see what it contains:

print "$username:\n";

If you did that, you would see:

sinan
:

indicating that $username has a newline at the end.

Sinan
 
L

Larry

You need to call "chomp" to get rid of the newline at the end of each
user response.

You can do it in 1 command, as in:
chomp($username=<STDIN>);

or in 2 commands:
$username=<STDIN>;
chomp $username;
 

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

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top