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


In reply to A regexp server in Perl by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.