I had this thought for a while, and finally got a bit free time to implement it.
I think everybody agrees that one of Perl's key strength is its regexp, which is something most of the other languages lack of. I had this idea to run a server written in Perl in our company. The server accepts TCP request to do m// or s///, and return the results to the clients. The clients can be written in any language, as long as that language support something like socket. Now virtually that language gains regexp power through Perl.
I would like to support two ways of making request: usual TCP calls and SOAP.
And I would like to hear your input at this early stage. Some of the proto type I have now:
The server in Perl: (modifiers will be considered later)
use IO::Socket::INET; use strict; use warnings; my $s = IO::Socket::INET->new(Proto => "tcp", LocalHost => "localhost", LocalPort => 3000, Timeout => 60, Listen => 10, Reuse => 1) || die "failed to start\n"; print "regexp service started\n"; while (1) { if (my $c = $s->accept()) { print "Got one service request\n"; my $func = readLine($c); if ($func eq "match") { my $string = readLine($c); my $pattern = readLine($c); if ($string =~ m/$pattern/) { print $c "1\n"; } else { print $c "0\n"; } } elsif ($func eq "substitute") { my $string = readLine($c); my $pattern = readLine($c); my $replacement = readLine($c); $string =~ s/$pattern/$replacement/; print $c "$string\n"; } close($c); } } sub readLine { my $c = shift; my $line = <$c>; $line =~ /(.*)\r\n/; return $1; }
The Java API (the wrapper):
import java.net.*; import java.io.*; class RegExp { public static boolean match(String string, String pattern) { try { Socket s = new Socket("localhost", 3000); BufferedReader in = new BufferedReader(new InputStream +Reader(s.getInputStream())); PrintWriter out = new PrintWriter(new BufferedOutputStream +(s.getOutputStream()), true); out.println(new String("match")); out.println(string); out.println(pattern); String data = in.readLine(); if (data.equals("1")) { return true; } else { return false; } } catch (IOException e) { System.out.print(e); return false; } } public static String substitute(String string, String pattern, Str +ing replacement) { try { Socket s = new Socket("localhost", 3000); BufferedReader in = new BufferedReader(new InputStream +Reader(s.getInputStream())); PrintWriter out = new PrintWriter(new BufferedOutputStream +(s.getOutputStream()), true); out.println(new String("substitute")); out.println(string); out.println(pattern); out.println(replacement); String data = in.readLine(); return data; } catch (IOException e) { System.out.print(e); return null; } } }
The Java Tester:
class RegExpTest { public static void main(String[] argv) { if (argv[0].equals("m")) { boolean result = RegExp.match(argv[1], argv[2]); System.out.print(result); } else if (argv[0].equals("s")) { String result = RegExp.substitute(argv[1], argv[2], argv[3 +]); System.out.print(result); } } }
You test like this: java RegExpTest "s" "123abc456" "\d" "-"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A regexp server in Perl
by itub (Priest) on Nov 10, 2004 at 23:06 UTC | |
by pg (Canon) on Nov 10, 2004 at 23:38 UTC | |
|
Re: A regexp server in Perl
by diotalevi (Canon) on Nov 10, 2004 at 23:21 UTC | |
by pg (Canon) on Nov 10, 2004 at 23:34 UTC | |
by erix (Prior) on Nov 11, 2004 at 00:05 UTC | |
|
Re: A regexp server in Perl
by Jenda (Abbot) on Nov 11, 2004 at 10:07 UTC | |
by iburrell (Chaplain) on Nov 11, 2004 at 18:31 UTC | |
|
Re: A regexp server in Perl
by Anonymous Monk on Nov 11, 2004 at 11:26 UTC | |
|
Re: A regexp server in Perl
by stvn (Monsignor) on Nov 11, 2004 at 15:32 UTC |