in reply to Re: Passing entered value into sub for IO::Prompt
in thread Passing entered value into sub for IO::Prompt

I tried using \&CheckDate, and it runs, but from my debug version of the sub CheckDate below, $date is printed as null , i.e. nothing is being passed in. If I use \&CheckDate($_), it gives the error message in the -require.
sub CheckDate { my $date = shift @_; print "Date: $date\n"; return true; }
Looking at the example, it seems I may have to use an anonymous subroutine instead? I figured it would be better for maintainability and reuse if I put in a separate CheckDate sub. That said, I tried putting it an anoymous sub, and get the following, which is always valid, even when I enter a **invalid** date (e.g. asagfas):
-require=>{ "Must be in the format (two di +gits per value) of MM/DD/YY: " #=> \&CheckDate($_) => sub {return (ParseDate($_)) + ? true : false } #=> sub {return true } }
What am I missing here?

-- Burvil

Replies are listed 'Best First'.
Re^3: Passing entered value into sub for IO::Prompt
by GrandFather (Saint) on May 28, 2008 at 00:34 UTC

    Use:

    sub CheckDate { my $date = $_; print "Date: $date\n"; return true; }

    and \&CheckDate(), in the -require.

    Note that the parameter list to prompt is built when the call to prompt is executed, but that the context that CheckDate is called from is way down in the processing of prompt somewhere. During the parameter processing part of the call to prompt the only thing of interest is the reference to the sub to be called.


    Perl is environmentally friendly - it saves trees
      I do that, and running the script jumps straight to the subroutine, even before a prompt, where there is nothing entered. Then the prompt follows and doesn't take a valid date. I get:
      $ ./pad_track.pl Date: Enter the day for this event, in format Must be in the format (two digits per value) of MM/DD/YY:

      -- Burvil