Wrapper application

S

s1037989

I want to write a Perl program that is a wrapper for another program.
The Perl program will stay running indefinitely and therefore I would
like the underlying program to stay running indefinitely as well. How
can I have my Perl program launch the other program and continuously
feed it data via stdout to the sub's stdin and read the sub's stdout on
my Perl's stdin without the sub exiting after each request?

Speaking less generically, for prototyping purposes, I want to expand
the capabilities of squidGuard using Perl. Therefore, the
redirector_program in squid will be my Perl program which I would like
to just be a wrapper around squidGuard. But I don't want to have to
relaunch squidGuard for EVERY request.
 
S

s1037989

Michele said:
IIUC you want IPC::Open2. But then I hope that when you write 'sub'
you mean the wrapped program. And of course you wouldn't be reading
its STDOUT from your script's STDIN, but from another handle. However
how could that possibly be a problem for you?

Thank you, Michele! That is EXACTLY what I needed!

For those who might stumble across this thread:

(Please ignore the uselessness and redunancy... This is a work in
progress and I just wanted to provide a real-life, working example for
the benfit of others. Again, this code base can be used to extend the
functionality of squidGuard -- not recommended outside of prototyping.)

use URI::Split qw(uri_split uri_join);
use IPC::Open2;

our $request;
our $response;
my $pid = open2(\*CHLD_OUT, \*CHLD_IN, '/usr/bin/squidGuard', '-c',
'/etc/squid/squidGuard.conf');
while ( chomp($request = <STDIN>) ) {
print CHLD_IN "$request\n";
chomp($response = <CHLD_OUT>);
$response = $request unless $response;

my ($uri, $dummy, $ident, $method) = split /\s+/, $response;
my ($scheme, $rfqdn, $path, $query, $frag) = uri_split($uri);
my $newuri = uri_join($scheme, $rfqdn, $path, undef, undef);
my ($cip, $lfqdn) = split /\//, $dummy;
my ($domain, $uid) = $ident =~ /%5c/ ? split /%5c/, $ident :
(undef, $ident);
($ident) = defined $domain ? "$domain\\$uid" : $uid;
$response = "$newuri $cip/$lfqdn $ident $method";

message(0, $request eq $response ? "+++ OK: $request" : "---
ERR: $response");
print $request eq $response ? "\n": "$response\n";
}

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
S

s1037989

Michele said:
Do not worry! We recommend people to prepare minimal but functional
examples all the time. Which I and many others simply regard as an act
of common sense. If only everybody shared your common sense...

Great! :)
(Do a favour to yourself and)

use strict;
use warnings;
Always!!

as well. Even in a minimal example, if it's not really, say, a
oneliner or so.


BTW: why do you want them as package variables?

Well, for the more fully functional application, I was intending a ton
of subroutines and I didn't want to have to pass those variables into
the subroutine on EVERY call. It makes for messy code... Is this very
frowned upon?
I would use lexical handles as is now commonly recommended with
standard open().

Please explain? Does that mean to just drop the \*?
Also

$response ||= $request;

Yeah... Sometimes the logic growing in my mind doesn't let me do the
extremely obvious things! Ha ha!
Err... message()? I'm afraid it's not a minimal, but *complete*, and
working example, after all... ;-)

I thought about this one right after I sent it. All I CnP'd was the
main while loop as that is what the focus of this thread is. But I
figure people, as you have demonstrated, are smart enough to figure
that one out if they do want to use this code sample. :D
(But since you actually did solve your problem, it shouldn't be a
problem itself now.)


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
P

Peter J. Holzer

Well, for the more fully functional application, I was intending a ton
of subroutines and I didn't want to have to pass those variables into
the subroutine on EVERY call. It makes for messy code... Is this very
frowned upon?

They can still be lexically scoped:

my $request;
my $response;

if you only access them in the same file.

Please explain? Does that mean to just drop the \*?

No. It means to use lexically scoped scalar variables:

my $pid = open2(my $chld_out, my $chld_in,
'/usr/bin/squidGuard',
'-c', '/etc/squid/squidGuard.conf');

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top