Hack 12. Use As Your UI


Hack 12. Use $EDITOR As Your UI

Nothing beats your favorite editor for editing text.

If you live on the command line and have a reputation for turning your favorite beverage[1] into code, you're likely pretty handy on the keyboard. If you're a relentless automator, you probably have dozens of little programs and aliases to make your life easier.

[1] Your author recommends peppermint tea.

Sometimes they need arguments. Yet beyond a certain point, prompting for arguments every time or inventing more and more command-line options just doesn't work anymore. Before you resign yourself to the fate of writing a little GUI or a web frontend, consider using a more comfortable user interface insteadyour preferred text editor.

The Hack

Suppose you have a series of little programs for updating your web site. Your workflow is to create a small YAML file with a new posting, then run that data through a template, update the index, and copy those pages to your server. Instead of copying a blank YAML file (or trying to recreate the necessary fields and formatting by hand), just launch an editor.

For example, a simple news site might have entries that need only a title, the date of posting, and a multiline block of text to run through some formatter. Easy:

use YAML 'DumpFile'; use POSIX 'strftime'; local $YAML::UseBlock = 1; exit 1 unless -d 'posts'; my @posts = <posts/*.yaml>; my $file  = 'posts/' . ( @posts + 1 ) . '.yaml'; my $fields = {     title => '',     date  => strftime( '%d %B %Y', localtime( ) ),     text  => "\\n\\n", }; DumpFile( $file, $fields ); system( $ENV{EDITOR}, $file ) = = 0     or die "Error launching $ENV{EDITOR}: $!\\n";

Assuming you have the EDITOR environment variable set to your preferred editor, this program creates a new blank post in the posts/ subdirectory with the appropriate id (monotonically increasing, of course), then drops you in your editor to edit the YAML file. It has already populated the date field with the current date in the proper format. Additionally, setting $YAML::UseBlock to a true value makes YAML treat the multiline text string as a YAML heredoc, making it much easier to edit.

Running the Hack

From the proper directory, just run the program. It will launch a new editor on the file. When you've finished editing, save and quit, and the program will continue.

This may work very differently on non-Unix systems.


Hacking the Hack

You don't have to give up on error checking even without a formal GUI. If you can't read in the YAML file or don't have all of the right fields filled in, you can rewrite the file with as much or as little information as you like, prompting the user to try again. You can even add comments or special fields to the file explaining the error.

To read in the file, just call LoadFile with the filenamethen continue as normal, as if the user hadn't had to create the file.



Perl Hacks
Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving
ISBN: 0596526741
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net