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

I'm trying to figure out how to pass the string from STDIN into the subroutine in the hashref for IO::Prompt, i.e. what do I need to enter into CheckDate below?
my $day = "\nEnter the day (MM/DD/YY):" , -require=>{ "Must be in the format (two digits per value) of MM/DD +/YY" => CheckDate(??what_here???) }; }
Update: I ended up using anonymous subroutines instead, since all CheckDate does is run ParseDate(). I simply put that in the anonymous sub, and it runs fine.

-- Burvil

Replies are listed 'Best First'.
Re: Passing entered value into sub for IO::Prompt
by GrandFather (Saint) on May 27, 2008 at 01:47 UTC

    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
      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