Unexpected chomp() results

Discussion in 'Perl Misc' started by Exide Arabellan, Feb 19, 2004.

  1. Greetings,

    In the following code im trying to remove the ending newline (\n) from
    $login_attempt and print it to $client.

    ---
    1. #! /usr/bin/perl -w
    2.
    3. use strict;
    4. use IO::Socket;
    5.
    #.. declaring variables and printing local info to <STDOUT> ..
    28.
    29. while (my $client = $server->accept()) {
    30. $client->autoflush(1);
    31. print $client "username: ";
    32. while (<$client>) {
    33. my $login_attempt;
    34. $login_attempt = $_;
    35. chomp($login_attempt);
    36. print $client "Account '".$login_attempt."' does not exist. Please
    try again.";
    37. }
    38. }
    ---

    Returns:
    ' does not exist. Please try again.

    Instead of:
    Account 'foo' does not exist. Please try again.

    I tried writing a simple test script using lines 33-36 (replacing $_
    with <STDIN> on line 35) and it worked as i had expected. Any thoughts?

    Ryan Zander
    www.arabellan.com
     
    Exide Arabellan, Feb 19, 2004
    #1
    1. Advertising

  2. Exide Arabellan

    J Krugman Guest

    In <> Exide Arabellan <> writes:

    >Greetings,


    > In the following code im trying to remove the ending newline (\n) from
    >$login_attempt and print it to $client.


    >---
    > 1. #! /usr/bin/perl -w
    > 2.
    > 3. use strict;
    > 4. use IO::Socket;
    > 5.
    > #.. declaring variables and printing local info to <STDOUT> ..
    >28.
    >29. while (my $client = $server->accept()) {
    >30. $client->autoflush(1);
    >31. print $client "username: ";
    >32. while (<$client>) {
    >33. my $login_attempt;
    >34. $login_attempt = $_;
    >35. chomp($login_attempt);
    >36. print $client "Account '".$login_attempt."' does not exist. Please
    >try again.";
    >37. }
    >38. }
    >---


    >Returns:
    >' does not exist. Please try again.


    >Instead of:
    >Account 'foo' does not exist. Please try again.



    What do you get if you print $login_attempt before chomping it?

    jill
     
    J Krugman, Feb 19, 2004
    #2
    1. Advertising

  3. J Krugman wrote:

    >
    > What do you get if you print $login_attempt before chomping it?
    >
    > jill
    >


    Account 'foo
    ' does not exist. Please try again.
     
    Exide Arabellan, Feb 19, 2004
    #3
  4. Exide Arabellan <> wrote:
    [...]
    > 4. use IO::Socket;

    [...]
    > 29. while (my $client = $server->accept()) {
    > 30. $client->autoflush(1);
    > 31. print $client "username: ";
    > 32. while (<$client>) {
    > 33. my $login_attempt;
    > 34. $login_attempt = $_;
    > 35. chomp($login_attempt);

    [...]
    > Returns:
    > ' does not exist. Please try again.
    > Instead of:
    > Account 'foo' does not exist. Please try again.



    Aren't lines sent over a TCP socket terminated with "\r\n"?
    If so, chomp is insufficient with the default $/, because it won't
    remove the \r.

    Set $/="\r\n" before line 29, or wrap that while loop with:
    {
    local $/ = "\r\n";
    while (my $client = $server->accept()) { ... }
    }


    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Feb 19, 2004
    #4
  5. Glenn Jackman wrote:

    > Aren't lines sent over a TCP socket terminated with "\r\n"?
    > If so, chomp is insufficient with the default $/, because it won't
    > remove the \r.
    >
    > Set $/="\r\n" before line 29, or wrap that while loop with:
    > {
    > local $/ = "\r\n";
    > while (my $client = $server->accept()) { ... }
    > }


    That was it :) I figured it was something to do with 'print'ing over a
    socket. Thanks for the help.
     
    Exide Arabellan, Feb 19, 2004
    #5
  6. Exide Arabellan

    Uri Guttman Guest

    >>>>> "EA" == Exide Arabellan <> writes:

    EA> 32. while (<$client>) {
    EA> 33. my $login_attempt;
    EA> 34. $login_attempt = $_;

    why all the copying and declaring?
    while (my $login_attempt = <$client>) {

    EA> 35. chomp($login_attempt);
    EA> 36. print $client "Account '".$login_attempt."' does not
    EA> exist. Please try again.";
    EA> 37. }
    EA> 38. }
    EA> ---

    EA> Returns:
    EA> ' does not exist. Please try again.

    EA> Instead of:
    EA> Account 'foo' does not exist. Please try again.

    you are probably reading a line with cr/lf ending from the server and
    chomping away only the lf (\n). chomp uses $/ which had a platform
    default. the symptom you see is the cr causing the line to overwrite
    part of itself when printing on a screen. the data was output, but you
    just don't see it all.

    several solutions. the simplest is to do a real strip with a regex:

    $login_attempt =~ s/[\r\n]+\z// ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 19, 2004
    #6
    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. Scott Lander

    Re: unexpected results

    Scott Lander, Jul 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,752
    Scott Lander
    Jul 7, 2003
  2. Dave
    Replies:
    1
    Views:
    349
    Leor Zolman
    Apr 8, 2004
  3. =?Utf-8?B?QXJ0?=

    Page inherting from .master - unexpected results

    =?Utf-8?B?QXJ0?=, May 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    372
    =?Utf-8?B?QXJ0?=
    May 26, 2006
  4. CK
    Replies:
    3
    Views:
    342
  5. martin
    Replies:
    3
    Views:
    201
    Joe Smith
    Apr 15, 2006
Loading...

Share This Page