Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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!

In reply to Re^3: Relationship between chomp() operator and STDIN by Random_Walk
in thread Relationship between Chomp() operator and STDIN by tty1x

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2019-09-21 20:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The room is dark, and your next move is ...












      Results (273 votes). Check out past polls.

      Notices?