problem when using <STDIN>

Discussion in 'Perl Misc' started by Paul, Jul 13, 2005.

  1. Paul

    Paul Guest

    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
     
    Paul, Jul 13, 2005
    #1
    1. Advertising

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


    <snip>

    > 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> );

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 13, 2005
    #2
    1. Advertising

  3. > 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 :

    chomp($username = <STDIN>);
    # ... and so on with the other variables.
    > 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>;
    >


    --
    "Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
    que je veux !"
    "The obvious mathematical breakthrough would be development of an easy
    way to factor large prime numbers." (Bill Gates, The Road Ahead)
     
    Stephane Zuckerman, Jul 13, 2005
    #3
  4. "Paul" <> wrote in news:1121267080.661220.294660
    @g43g2000cwa.googlegroups.com:

    > 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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jul 13, 2005
    #4
  5. Paul

    Larry Guest

    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;
     
    Larry, Jul 13, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,471
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    825
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,412
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    455
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    316
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page