Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

The text preceding <STDIN> is not printed

by c_config (Initiate)
on Aug 17, 2016 at 18:57 UTC ( [id://1169940]=perlquestion: print w/replies, xml ) Need Help??

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

I apologize for maybe asking a dummy question. I've just started learning Perl on my own and no one around to ask. I've created the script which should prompt to enter the hex digits and then convert them in to decimal. Here it is:
#!/usr/bin/perl -w use strict; use warnings; print "Please, enter the hex number: \n"; my $hex = <STDIN>; print "$hex", "\n"; chomp ($hex); print "You've entered hex: ", $hex, "\n"; print "The decimal equivalent is ", hex "$hex", "\n";
Generally saying, this script works correctly. However, whatever I've tried, I could not make this first prompting text to be printed. I've just submit my hex digits blindly and receive a correct result. This is what was returned for hex FF:
ff Please, enter the hex number: You've entered hex: ff The decimal equivalent is 255
I wanted to ask why that prompting text "Please, enter the hex number:" is not printed?

Replies are listed 'Best First'.
Re: The text preceding <STDIN> is not printed
by afoken (Chancellor) on Aug 17, 2016 at 19:09 UTC

    Did you notice that your posting is hard to read when you pressed the PREVIEW button? That's because you did not wrap your code in <code> tags. Not a big problem, you can edit your posting right now, simply insert <code> before your code and </code> after your code.

    And by the way, you seem to be suffering from buffering.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Why should that be the case? I read on the linked page that print() calls on terminal is line buffered, so why there is buffering asks for another reason. Or is this phanomenon platform-dependent?

      If you run this, you find that it works the way you expect: The prompt appears on the screen right away. Where's the buffering? Why didn't Perl save up the output until it had a full buffer? Because that's almost never what you want when you're writing to a terminal, the standard I/O library that Perl uses takes care of it for you. When a filehandle is attached to the terminal, as STDOUT is here, it is in line buffered mode by default. A filehandle in line buffered mode has two special properties: It's flushed automatically whenever you print a newline character to it, and it's flushed automatically whenever you read from the terminal. The second property is at work here: STDOUT is flushed automatically when we read from STDIN.

      Short test:

      % perl print "Hi. Your name, please: "; chomp( my $name = <STDIN> ); print "Oh, $name, I think I am your system. Glad to be at your service +.\n"; # Press CTRL-D or whatever shortcut to trigger EOF Hi. Your name, please: Florian Oh, Florian, I think I am your system. Glad to be at your service.

        The OP states in Re: The text preceding <STDIN> is not printed that it helped.

        Perhaps he runs his Perl scripts from a special environment, e.g. an IDE, which tees the script's STDOUT, so that Perl doesn't recognize it as a terminal any more…
Re: The text preceding <STDIN> is not printed
by c_config (Initiate) on Aug 17, 2016 at 20:23 UTC
    As more experienced programmers pointed out (thank you to afoken!!!), I've had the buffering issue. After reading this article at perl.plover.com/FAQs/Buffering.html, I've applied the filehandle HOT. Now my script works as expected and looks like this:
    #!/usr/bin/perl -w use strict; use warnings; print "Please, enter the hex number : \n"; S|=1; my $hex = <STDIN>; print "$hex", "\n"; chomp ($hex); print "You've entered hex: ", $hex, "\n"; print "The decimal equivalent is ", hex "$hex", "\n";
    Thank you very much for the help!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1169940]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found