Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Execute selected text as python or perl UNIX
In writing python code or perl in an editor, I often want to execute a tiny piece of the code in a standalone fashion as a way of debugging it. To facilitate this, I wrote a little perl script that allows me to execute any text selected in any Macintosh editor over in a Terminal window.

Copy and paste this script into a text editor, save it as python_execute.pl or perl_execute.pl, and make it executable (chmod a+x python_execute.pl). Edit the file as indicated to switch it from targeting perl to targeting python, as you prefer. To use it, select text in any editor, and select the Copy function from the editor's Edit menu. This loads the text into the clipboard (AKA the pasteboard). Then in any terminal window type:
pbpaste | python_execute.pl
And it will print the selected text to the window, save it to a temporary file (in /tmp), and then execute that file as a python file. The resulting error message, or any output from the execution, is printed to the Terminal window.

If you have BBEdit, place this script in ~/Library/Application Support/BBedit/Unix Filters, and it will be available directly from BBEdit, under your Unix Scripts menu. You won't need to do the Terminal paste to run it, and BBEdit will open a window to show you any error messages or output returned.

[robg adds: I haven't tested this one...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[12,117 views]  

Execute selected text as python or perl | 11 comments | Create New Account
Click here to return to the 'Execute selected text as python or perl' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Execute selected text as python or perl
Authored by: waffffffle on Mar 11, '05 11:09:25AM

This would be cool if it was available as a system service, so no cut and pasting needed.



[ Reply to This | # ]
Execute selected text as python or perl
Authored by: adrianm on Mar 11, '05 12:09:17PM
Services can be done as normal scripts/executables, so no programming required.

http://www.crackpot.com/geekfiles/cocoa-filter.html shows how.

[ Reply to This | # ]

Execute selected text as python or perl
Authored by: SOX on Mar 11, '05 12:31:34PM

No need for pasting! just copy the text, then execute the terminal script. It reads the pasteboard. Obviously the first time you execute the script there's some typeing. But other executions just require you to press up-arrow, return in the terminal window.

Would be nice to have a service for this instead but it might not be significantly more convenient.



[ Reply to This | # ]
Python Service
Authored by: Lankhmart on Mar 11, '05 01:06:36PM
For Python, there is PyService. (Complete with source code. A Perl user with a little bit of Cocoa knowledge could probably just make a couple of changes to the NSTask arguments.)

[ Reply to This | # ]
Execute selected text as python or perl
Authored by: sheepmaster on Mar 11, '05 12:11:07PM

If you use BBEdit (or TextWrangler), you can actually execute a perl script directly via the "#!" menu (Don't know about python though).

And in the terminal, you could just do pbpaste | perl or pbpaste | python to execute the contents of the clipboard.



[ Reply to This | # ]
Execute selected text as python or perl
Authored by: SOX on Mar 11, '05 01:01:48PM

You're right about the trick of using "pbpaste | perl ". I like it!

I like it! that is a better solution than using the script at the command line.

However, in bbedit you can only execute a whole file using #!>run menu item. To execute only the selected text then you need to create a unix filter like I did.

But following your suggestion one could make a simpler bbedit script as follows:
#!/usr/bin/sh
pbpaste | perl -w

and save that in the bbedit unix scripts section so it appears under the #! menu.

the difference between this and the script I provided is that to activate the the above script one has to actually put the selected text in the pasteboard (press command-C) whereas the script I provided only required the text to be selected in bbedit. It did not have to be in the pasteboard to work in bbedit.

nice suggesions.

now if someone would just write this up as a service menu item.



[ Reply to This | # ]
Execute selected text as python or perl
Authored by: cedric on Mar 11, '05 12:55:17PM
While the idea is very clever (and I am a bit ashamed of not having thought of it first), I always thougt that Perl people are doing fixes the hard way.

You can use pbpaste, that's very cool. But you can do what you want simply by putting a

 | python - 
afterwards.

If, like me, you need some home-made definitions used thousands of times every day and stored in a file, you can set your

PYTHONSTARTUP
variable to a startup.py file saying
from my_module_with_1000_useful_defs import *

Then, use this:

 pbpaste | python -i 

Et voilĂ ! The -i ensures python starts up and reads the startup module, and your piece of code stored in pbpaste can now call any of the defs defined in your module. And no need for another cryptic Perl script.

[ Reply to This | # ]

Execute selected text as python or perl
Authored by: borisz on Mar 11, '05 01:53:10PM
Hi, for perl there is already another tool that execute perl and put the results back into the pastboard ( and more ). http://perl-pad.sourceforge.net/.

[ Reply to This | # ]
Execute selected text as python or perl
Authored by: neror on Mar 11, '05 02:29:11PM
TextMate can do this for you with Ctrl-R. For example select the text:

ruby -e "print \"\nHello TextMate\n\""

and hit Ctrl-R and you end up with this:

ruby -e "print \"\nHello\n\""
Hello TextMate


I haven't found it super useful yet, but it does what you're talking about without any cutting and pasting. It actually executes the selected test in it's own shell environment, and you can set environment variables and everything. Pretty cool.

I'm not affiliated, but TextMate is available at macromates.com. It costs about $40, but it has a 30 day trial.

[ Reply to This | # ]
General usage
Authored by: SOX on Mar 11, '05 04:58:32PM

As a number of people have pointed out, perl and python have standard input modes that permit a simpler version of the hint to exist. However the general principle of the hint works for any language (octave, r, bash, ruby...) that can execute a temporary file.

The guts of the script contains just four simple elements. Capturing a standard input stream, reformatting carriage returns to linefeeds, generating a uinquely named temporary file. Then it executes this file and
prints the results to the standard error.

So in principle executing the file using any command language is only a change of the command name needed to run the language of choice.

Note there is one non-obvious flourish. The command prints the the Standard input back to the strandard output and it sends all other text to the standard error. Both of these steps are critical if you want to use it as a filter and not simply run it at the command prompt. For example in bbedit if you were to not print out the text that was read in then when run as a filter it would delete your selected text! If the output were sent to the STDOUT rather than the standard error then in filter mode in bbedit it would append the error messages and output text to the slected text in your editor window.

For bash, perl and python, and using a terminal window as was pointed out this script is not needed since one can do:
pbpaste | perl -w
or
pbpaste | pthyon
or, for any shell language, just
pbpaste


but for other less convenient command interpreters this script is needed.

for bbedit the unix_script file could be simplified to
#!/usr/bin/sh
pbpaste | perl -w

the only difference between the above and the script I provided is that now you have to copy the text in bbedit, not just select it, so that it is placed in the pasteboard. And you also have to make sure you run it as a bbedit unix_script and not as a Unix_filter to avoid deleting your original text since the above script does not echo the original text back.




[ Reply to This | # ]
Execute selected text as python or perl
Authored by: taxi on Mar 13, '05 10:49:38PM
Creating files in /tmp ican be dangerous: see here

[ Reply to This | # ]