[ale] Using Expect.pm
Christopher Fowler
cfowler at outpostsentinel.com
Tue Aug 3 13:35:44 EDT 2004
Here is how I solved it using Expect. Expect is what I'm using on the
streams and I do not believe it supports perl regular expressions.
--- Object ---
package SAM::Util;
use Expect;
use strict;
sub test_regex {
my ($self, $reg) = @_;
my $pid = fork();
do {
wait;
return ( $? ? undef : 1);
} if $pid;
open(F, " < /dev/null");
my @R = ();
my $handler = sub {
my $self = shift;
exp_continue;
};
push @R, [ '-re', $reg, $handler];
my $exp = Expect->exp_init(\*F);
eval {
$exp->expect(0, at R);
};
if($@) {
exit 1;
}
exit 0;
}
return 1;
--- Test code ---
use SAM::Database;
use SAM::Util;
use Getopt::Std;
use strict;
#
# Static variables
#
my $test_regex = undef;
my $database = undef;
sub args {
getopts('t:d:');
$test_regex = $Getopt::Std::opt_t;
$database = $Getopt::Std::opt_d;
return;
}
args;
do {
my $db = SAM::Database->new();
$db->connect($database);
my @T = $db->get_triggers();
foreach my $ref (@T) {
next unless $ref->{'criteria_type'} eq 'R';
print "$ref->{'name'}: ";
my $res = SAM::Util->test_regex($ref->{'criteria'});
print ( $res ? "syntax ok" : "syntax incorrect" );
print "\n $ref->{'criteria'}\n" unless $res;
print "\n";
}
exit 0;
} if $database;
die "regex_text.pl <regular express>\n" unless $test_regex;
my $res = SAM::Util->test_regex($test_regex);
print ( $res ? "syntax ok" : "syntax incorrect" );
print "\n";
# vi: set ts=2 sw=2: #
On Tue, 2004-08-03 at 13:16, Geoffrey wrote:
> I'm really enjoying the Perl tutorials from Fletch, but I'm thinking
> it's about time he billed Christopher. :)
More information about the Ale
mailing list