Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^3: Using IO::Prompt's menu

by TheDamian (Priest)
on Oct 01, 2005 at 01:34 UTC ( #496579=note: print w/replies, xml ) Need Help??

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 Just ask!!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://496579]
[stevieb]: ...has honed in my skills of recognizing sound
[stevieb]: All of the early members are coming out of the woodwork today :) Hey, planetscape
[Corion]: This cover version had so much promise but the singing is underwhelming :-/
[stevieb]: erix Thanks! I dislike remakes of songs usually, but my favourite remix of guitar/weeps is Jeff Healey. I'll take a listen to yours
[planetscape]: hi stevieb!
[stonecolddevin]: also hi Corion
[Corion]: stevieb: Yeah, but I don't follow guitar players enough... I never got Prince and I don't think I could recognize his play style. I can recognize Santana and Nile Rodgers, but that's about it, and not really deep knowledge I think ;)
[stevieb]: Jeff Healey cut of My guitar gently weeps
[stonecolddevin]: you don't recognize Prince. Prince tells you it's him.
[erix]: Alvin Lee (Ten Years After) played a jazzguitar ("Big Red"), a good warm sound

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2017-06-22 21:30 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.