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

A fair guess is that you don't need anything there. Either the entered string will be passed to CheckDate or it is in $_ when CheckDate is called.

However you probably need to use \&CheckDate rather than CheckDate().

Update: the record.pl example confirms that the entered string is passed in $_. Note that, apart from the comment at the start of the sample code, the record.pl and require.pl examples are identical.


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: Passing entered value into sub for IO::Prompt
by bowei_99 (Friar) on May 28, 2008 at 00:09 UTC
    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

      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