Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Try this:

#!/usr/bin/env perl use strict; use warnings; use IO::Prompt; sub shall_we { my $ans = prompt( "What'll it be?", -menu => { 'Yes, definitely.' => 1, 'No way.' => 0 }, ); print "Inside function, you chose: $ans.\n"; return $ans; } if ( shall_we() ) { print "In the 'if' statement: yes, we shall.\n"; } else { print "In the 'if' statement: no, we shant.\n"; }

On my system, it always tells me "yes, we shall", regardless of which option I choose.

I've got IO::Prompt 0.99.4 and this is Perl 5.10.0.

Replies are listed 'Best First'.
Re: IO::Prompt -- why isn't this working (menu with hashref)?
by Fletch (Bishop) on Jun 20, 2008 at 21:24 UTC

    IO::Prompt is returning an instance of IO::Prompt::ReturnVal, not a plain scalar value. Apparently this instance isn't boolean-ifying the way you expect (not that I disagree with your expectation after a quick initial read of the docs) and since it's not false you always get your first leg of your if executing.

    Update: As a workaround you can explicitly stringify return $ans . "" or numify it return $ans + 0 and it should behave the way you want. Alternately explicitly test the value (e.g. if( shall_we() == 1 ) { ... } else { ... }).

    Another way: Since you're using 5.10 there's always given:

    use feature qw( switch say ); ## ... as your code ... given( shall_we() ) { when (1) { print "In the 'if' statement: yes, we shall.\n"; } when (0) { print "In the 'if' statement: no, we shant.\n"; } };

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Personally, I'd go with:

      sub shall_we { my $ans = prompt( "What'll it be?", -menu => { 'Yes, definitely.' => 1, 'No way.' => 0 }, ); return $ans ? 0+$ans : undef; }

      not that I disagree with your expectation after a quick initial read of the docs

      Or even after a thorough read. The returned object is not documented at all. From peeking at the source,

      • It returns whether prompt was successful (whatever that means) when used in boolean context.
      • It returns the answer as a string when used in string context.
      • It returns the answer as a number when used in numerical context.

      It has no methods.

      It has a very weird destructor that clobbers $_ if you don't do one of the above before the object is destroyed. That allows the following to work:

      sub shall_we { local $_; prompt( "What'll it be?", -menu => { 'Yes, definitely.' => 1, 'No way.' => 0 }, ); return $_; }

      Update: Fixed ... : ... : ... that should have been ... ? ... : ...

Re: IO::Prompt -- why isn't this working (menu with hashref)?
by andreas1234567 (Vicar) on Jun 23, 2008 at 05:13 UTC
    The module author responded to this related bug with a beta [that] provides a -raw flag that causes prompt() to return the raw text that was typed in (as a string), rather than a RetVal object.

    However, it looks like this -raw flag never made it to the current version of IO::Prompt.

    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]