http://www.perlmonks.org?node_id=1031312

tty1x has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl –w print "Please enter your name\n"; chomp($name = <STDIN>); print "Thank you $name";
The output I expected after entering 'aaa' as input :
Please enter your name aaaThank you aaa[root Shared]#
but the output came up to be :
Please enter your name aaa Thank you aaa[root Shared]#
I assumed Chomp has already removed the /n character for STDIN, why has the next print still print on a new line ?

Replies are listed 'Best First'.
Re: Relationship between chomp() operator and STDIN
by Athanasius (Archbishop) on Apr 30, 2013 at 04:43 UTC

    Hello tty1x, and welcome to the Monastery!

    The call to <STDIN> reads a line of text from the keyboard — it doesn’t complete until the user enters the return character, which is echoed to the screen. Then chomp removes the trailing newline from the string $name which has been initialised with the user-entered text. chomp has no effect on STDIN, only on the string variable $name.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      thanks for the reply, it cleared up :) Just another query. Is it that for every scalar variable there is a trailing newline ? Or is it that the input from STDIN contains a trailing newline ?

        A scalar variable can hold just about any reasonable value, and is not inherently newline terminated. However, when input comes from a file or keyboard, the newline is often the "record separator", and as such will be included in the input.

        Note, other record separators are also possible for files, and legal, though not as ubiquitous. perlvar mentions that $/ is used to set the input record separator for the currently selected input filehandle. If an input file contained records delimited by | (pipe character), you could set local $/='|';, for example, and in that case, chomp would chomp trailing | characters instead of trailing newlines.


        Dave

        Scalar variables don't need to have a trailing newline. Unlike C where a string is normaly terminated with a nul, in Perl the string data structure stores the length of the string.

        What is happeneing with your code is this:

        As you type STDIN picks up each character and displays it to the screen a,a,a,\n are all displayed in turn.

        The \n causes the newline/cariage return on the screen and tells STDIN that a record is ready to hand off to your code.

        Now youre code runs chomp on $name and removes any instances of the input line seperator from the end of your string. By default the input line seperator is "\n", but you can change it by altering the value of $/

        finaly your code prints Thank you $name; # no newline in a $name anymore and exits. The OS gives you back a prompt.

        If you read from a file containing aaa\n instead of STDIN the output of your code would be the same, but as you are not using the keyboard to enter the data you would miss the initial line of aaa\n being shown on the display

        #!/usr/bin/perl print "Please enter your name\n"; chomp($name = <DATA>); # read a line from this file print "Thank you $name"; __DATA__ aaa A new line to show the above ends in \n

        Output

        R@Che ~/PerlTest $ ./name.pl Please enter your name Thank you aaaR@Che ~/PerlTest $

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!
Re: Relationship between Chomp() operator and STDIN
by NetWallah (Canon) on Apr 30, 2013 at 05:10 UTC
    Try entering this on the command line before you run your perl script:
    stty -echo
    You will not be able to see what you type - just the prompts and responses from your code.
    This will get you the output you seek.

    To reset to "normal", type "stty echo".

    If you are on Windows, the equivalent commands are "echo off" and "echo on".

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs