[TxMt] Re: Interactive input in Textmate (for Perl)

Alex Ross z-textmate at lasersox.net
Mon Mar 8 03:46:03 UTC 2010


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


More information about the textmate mailing list