Hi guys,
When I was trying to do some interactive input with Perl in TM, I got some weird output.
For example, I use this script:
#!/usr/bin/perl -w $pi = 3.14; print "What is the radius? "; chomp($radius = <STDIN>); $circ = 2 * $pi * $radius; print "The circumference of a circle of radius $radius is $circ.\n";
When I run the script and input "4", the output is this:
4 What is the radius? The circumference of a circle of radius 4 is 25.12.
I mean, when I input "4", I don't see the msg "What is the radius?". Shouldn't TM first show this msg and then, as the user, I input "4"?
I know that there is some issue with interactive input in TM (http://blog.macromates.com/2009/interactive-input/). Before I made the changes mentioned in that thread, I was even not able to use interactive input in TM. Now I can, but it just seems weird as mentioned above. And if I run the script in the Terminal, everything is OK - I first got a msg "What is the radius?", then I input whatever I want.
Any ideas about this?
Thanks, Jon
On Mar 7, 2010, at 7:27 PM, Jon2010 wrote:
When I was trying to do some interactive input with Perl in TM, I got some weird output.
For example, I use this script:
#!/usr/bin/perl -w $pi = 3.14; print "What is the radius? "; chomp($radius = <STDIN>); $circ = 2 * $pi * $radius; print "The circumference of a circle of radius $radius is $circ.\n";
When I run the script and input "4", the output is this:
4 What is the radius? The circumference of a circle of radius 4 is 25.12.
I mean, when I input "4", I don't see the msg "What is the radius?". Shouldn't TM first show this msg and then, as the user, I input "4"?
Any ideas about this?
This is the expected behavior. :)
When you print “What is the radius?” The output buffer isn't flushed, because that string doesn't end with a newline. Then, your script reads on stdin and the dialog pops up. The “4” is printed (in italics), because interactive input echos your typing.
The behavior is slightly different in Terminal because there perl knows it is connected to a TTY, so it doesn't buffer it's output.
You can disable buffering by placing this simple line of code at the start of your script:
if ($ENV{'TM_PID'}) { $| = 1 };
This produces the behavior you are expecting.
—Alex
Alex:
Thanks very much for the explanation! I appreciated it!
Jon
Alex Ross-11 wrote:
On Mar 7, 2010, at 7:27 PM, Jon2010 wrote:
When I was trying to do some interactive input with Perl in TM, I got some weird output.
For example, I use this script:
#!/usr/bin/perl -w $pi = 3.14; print "What is the radius? "; chomp($radius = <STDIN>); $circ = 2 * $pi * $radius; print "The circumference of a circle of radius $radius is $circ.\n";
When I run the script and input "4", the output is this:
4 What is the radius? The circumference of a circle of radius 4 is 25.12.
I mean, when I input "4", I don't see the msg "What is the radius?". Shouldn't TM first show this msg and then, as the user, I input "4"?
Any ideas about this?
This is the expected behavior. :)
When you print “What is the radius?” The output buffer isn't flushed, because that string doesn't end with a newline. Then, your script reads on stdin and the dialog pops up. The “4” is printed (in italics), because interactive input echos your typing.
The behavior is slightly different in Terminal because there perl knows it is connected to a TTY, so it doesn't buffer it's output.
You can disable buffering by placing this simple line of code at the start of your script:
if ($ENV{'TM_PID'}) { $| = 1 };
This produces the behavior you are expecting.
—Alex
textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate