January 2013 Archives

Windows CPAN Testers, your help please...

I recently blogged about Ask, my new(ish) module for interacting with users via STDIN/STDOUT, a GUI, or whatever means possible.

Writing GUI code in Perl unfortunately requires you to compile non-core modules such as Gtk2, Wx or Tk. There is an alternative no-compilation-required approach though: most modern Linux and BSD distributions ship with (or at least package for separate download) a program called Zenity. Zenity is a tool for adding basic GUI dialogue boxes to shell scripts. You call it like this:

zenity --file-selection

And it will display a standard file chooser. Once a file is chosen, Zenity will print the file path to STDOUT and then exit.

Ask provides Ask::Zenity, a wrapper around Zenity, and will fall back to that if Wx, Gtk2, Tk, etc are not available. However, none of this really addresses Windows...

Ask not what your user can do for you...

In many scripts, we need to prompt the end user for information - this could be a prompt for a file name, a selection from a list of options, or an answer to a yes/no question.

The traditional approach to this sort of question is to print your question to STDOUT, read a line from STDIN, and apply some sort of parsing to the answer...

   use 5.010;
   use strict;
   use warnings;
   my $answer;
   until (defined $answer) {
      print "Would you like fries with that?\n";
      $_ = <>;
      $answer = 1 if /^Y/i;
      $answer = 0 if /^N/i;
   say "Adding fries!" if $answer;

One issue with this approach is: what happens when your script is not running in a terminal?

w00t! First CPAN upload of 2013!

According to metacpan.org, MooX-ClassAttribute-0.001 was the first upload of 2013! Yay!

About Toby Inkster

user-pic I'm tobyink on CPAN, IRC and PerlMonks.