http://www.perlmonks.org?node_id=496579


in reply to Re^2: Using IO::Prompt's menu
in thread Using IO::Prompt's menu

But I am still puzzled as to why IO::Prompt would do that.

IO::Prompt is designed to be a drop-in replacement for the "diamond operator" (i.e. for: <>). It writes out its prompt to STDOUT and then does a <> to get the reply.

But the diamond operator is just a shorthand for <ARGV>, a readline on the magic filehandle that normally is attached to STDIN...unless you specify a filename on the commandline.

So it shouldn't really be a surprise that, if you put something on the commandline (as you typically do when running this program), <ARGV> attempts to open-and-read from that something. That's just standard Perl behaviour...which prompt() valiantly tries to preserve.

Of course, standard Perl behaviour isn't always what you want (as in this example). And that's why prompt() also has a -tty option, so you can explicitly say "Don't work like Perl's <ARGV> works; always read from the terminal instead".

You might like to try replacing:

my $choice = prompt 'Please choose your format...', -1, -menu => [ 'PAL', 'NTSC', 'Quit', ];

(which means "Prompt, reading from wherever <ARGV> reads from") with this:

my $choice = prompt -tty, 'Please choose your format...', -1, -menu => [ 'PAL', 'NTSC', 'Quit', ];

(which means "Prompt, always reading from the terminal").

If you are presenting a menu, you want the user to select something, not pass in an option on the commandline. If you did, you would use Getopt::* for that.

That's not quite what's happening here. If <ARGV> (or prompt()) finds a string on the commandline, it tries to open the file whose name is that string, and then reads from that file.

Why would you want that? Well, you might want that behaviour because you've written a script that automates the use of your program, and you want to feed the program its required input from a "prerecorded" file, which you specify on the commandline. Again, this is standard Perl behaviour, not my invention. I'm merely trying to preserve it when replacing <ARGV>.

As you can see, "least surprise" is a difficult thing to achieve. It depends entirely on what people find surprising, which in turn depends on what features of Perl they're familiar with.

However, it certainly would be a good idea for me to rework the docs a little to emphasize the "just like <ARGV>" behaviour of the module, with all that implies.

Thanks for bringing this "surprise" to my attention.

Replies are listed 'Best First'.
Re^4: Using IO::Prompt's menu
by ghenry (Vicar) on Oct 01, 2005 at 07:08 UTC

    "It depends entirely on what people find surprising, which in turn depends on what features of Perl they're familiar with."

    I think this says it all for me ;-)

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!