Hello,
I'd like to propose two changes to the AGI::get_data function. I hope
everyone agrees on the following patch:
--- AGI.pm Wed Mar 23 00:41:57 2005
+++ AGI.pm.orig Thu Aug 7 20:04:01 2003
@@ -340,7 +340,7 @@
return -1 if (!defined($filename));
$ret = $self->execute("GET DATA $filename $timeout $maxdigits");
- $self->callback($ret) if ((defined $ret) && $ret == -1);
+ $self->callback($ret) if ($ret == -1);
return $ret;
}
What it does is to get rid of a warning if you use -w in the perl
script that calls the agi.
The other proposal is more of an idea, actually. Some comments would
be great:
I recently wrote an AGI script where I wanted to differentiate between
the user typing '#' and a timeout where the user had entered nothing.
It turns out that the AGI::get_data function can not differnetiate
between the two cases, so I ended up with this code:
sub extendedGetData {
my $agi = shift;
my $streamfile = shift;
my $timeout = shift;
my $digits = shift;
my $data = $agi->get_data($streamfile, $timeout, $digits);
$data = "" if !defined $data;
if (wantarray) {
my $timeout = $agi->_lastresponse =~ /\(timeout\)/;
return ($data, $timeout);
} else {
return $data;
}
}
Except the fact that an empty string is returned instead of undef,
this is functionally equivalent to AGI::get_data when used in a scalar
context. (There's also a syntactic difference in that $agi is passed
as the first parameter.) It is also possible to write
($vote, $timeout) = extendedGetData($AGI, "whats-your-vote", 0, 3);
to get a boolean $timeout flag that tells if the prompt timed out or
if the user ended with a # sign. (The prompt says "Enter your vote,
or press pound to go back to the main menu.") It is now possible to
check
($vote eq "" && !$timeout)
to determine if the user typed '#' before a timeout occurred. If the
entered string is "" while there was a timeout, the prompt would be
repeated.
I believe that the important change, that is, being able to determine
if a timeout occurs, could be incorporated into the get_data function
without altering the current behavior, since the function is probably
always used in a scalar context currently. The proposed patch,
including both changes, is this:
--- AGI.pm.orig Wed Jun 15 21:43:40 2005
+++ AGI.pm Wed Jun 15 21:47:09 2005
@@ -340,9 +340,14 @@
return -1 if (!defined($filename));
$ret = $self->execute("GET DATA $filename $timeout $maxdigits");
- $self->callback($ret) if ($ret == -1);
+ $self->callback($ret) if ((defined $ret) && $ret == -1);
- return $ret;
+ if (wantarray) {
+ my $timeout = $self->_lastresponse =~ /\(timeout\)/;
+ return ($ret, $timeout);
+ } else {
+ return $ret;
+ }
}
=item $AGI->set_callerid($number)
Comments?
Staffan