Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   File handlings (http://www.velocityreviews.com/forums/t884984-file-handlings.html)

Glodalec 02-03-2004 02:13 PM

File handlings
 
Hi !

My B program is a simple script, which reads a string from STDIN,
converts it to uppercase and prints it to STDOUT.

B.pl
#!/bin/perl
use strict ;
my $string=<STDIN>;
print STDOUT uc $string,"\n" ;

Now, I have A program, which listens on a TCP socket. Whenever string
come over socket, it should be put on STDIN buffer (yes, STDIN), forking
a new child, redirecting the parent SOCKET to child's STDOUT and exec
B.pl. (The same way Apache & CGI modules work). I was trying to use
select() but no luck.

A.pl
#!/bin/perl
use strict ;
....
SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE
....

TCP settings....
.......
if (my $pid=fork)
{
waitpid($pid,0); # Ok, here is parent
} else
{
my $string="This must be put in STDIN buffer" ; << Don't know how
my $old=select(SOCKET) ; << This is not working
exec("A.pl"); # and child
select $oldstdout ;
}

Any help would be appreciated

Ben Morrow 02-03-2004 03:21 PM

Re: File handlings
 

Glodalec <glodalec@yahoo.com> wrote:
> Now, I have A program, which listens on a TCP socket. Whenever string
> come over socket, it should be put on STDIN buffer (yes, STDIN), forking
> a new child, redirecting the parent SOCKET to child's STDOUT and exec
> B.pl. (The same way Apache & CGI modules work). I was trying to use
> select() but no luck.


No, that's not how filehandles work at all. You can't push stuff back
onto them: if you want to pass stuff to your child you have to use a
pipe.

> A.pl
> #!/bin/perl
> use strict ;
> ...
> SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE
> ...
>
> TCP settings....
> ......
> if (my $pid=fork)


fork can fail, in which case it returns undef.

> {
> waitpid($pid,0); # Ok, here is parent
> } else
> {
> my $string="This must be put in STDIN buffer" ; << Don't know how
> my $old=select(SOCKET) ; << This is not working
> exec("A.pl"); # and child
> select $oldstdout ;
> }
>
> Any help would be appreciated


my $pid = open my $TOCHILD, '|-', 'A.pl'
or die "fork failed: $!";

print $TOCHILD "The child will read this on its STDIN";

See perldoc -f open.

Ben

--
For the last month, a large number of PSNs in the Arpa[Inter-]net have been
reporting symptoms of congestion ... These reports have been accompanied by an
increasing number of user complaints ... As of June,... the Arpanet contained
47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] * ben@morrow.me.uk

Glodalec 02-03-2004 07:13 PM

Re: File handlings
 
In article <bvoe9t$t31$1@wisteria.csv.warwick.ac.uk>,
usenet@morrow.me.uk says...
>
> Glodalec <glodalec@yahoo.com> wrote:
> > Now, I have A program, which listens on a TCP socket. Whenever string
> > come over socket, it should be put on STDIN buffer (yes, STDIN), forking
> > a new child, redirecting the parent SOCKET to child's STDOUT and exec
> > B.pl. (The same way Apache & CGI modules work). I was trying to use
> > select() but no luck.

>
> No, that's not how filehandles work at all. You can't push stuff back
> onto them: if you want to pass stuff to your child you have to use a
> pipe.

Had never used them (except on command line), will read about them.
>
> > A.pl
> > #!/bin/perl
> > use strict ;
> > ...
> > SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE
> > ...
> >
> > TCP settings....
> > ......
> > if (my $pid=fork)

>
> fork can fail, in which case it returns undef.

I know, this script has been arranged a lot when posting here.
>
> > {
> > waitpid($pid,0); # Ok, here is parent
> > } else
> > {
> > my $string="This must be put in STDIN buffer" ; << Don't know how
> > my $old=select(SOCKET) ; << This is not working
> > exec("A.pl"); # and child
> > select $oldstdout ;
> > }
> >
> > Any help would be appreciated

>
> my $pid = open my $TOCHILD, '|-', 'A.pl'
> or die "fork failed: $!";
>
> print $TOCHILD "The child will read this on its STDIN";

THanks for advice. How about a child exec, which should actually write
to parent's SESSION handler (got from accept()), instead of its own
STDOUT.?
>
> See perldoc -f open.
>
> Ben
>
>


Ben Morrow 02-03-2004 10:43 PM

Re: File handlings
 

Glodalec <glodalec@yahoo.com> wrote:
> > print $TOCHILD "The child will read this on its STDIN";

> THanks for advice. How about a child exec, which should actually write
> to parent's SESSION handler (got from accept()), instead of its own
> STDOUT.?


It's probably easiest to do that manually, viz (untested):

accept my $NEWCONN, $LISTENER or die "accept failed: $!";

# or you'd probably be better off using IO::Socket::INET
my $NEWCONN = $LISTENER->accept or die "accept failed: $!";

pipe my ($FROMPARENT, $TOCHILD) or die "pipe failed: $!";

my $pid = fork;
defined $pid or die "fork failed: $!";
unless ($pid) {
open STDIN, '<&', $FROMPARENT or die "dup2 to STDIN failed: $!";
open STDOUT, '>&', $NEWCONN or die "dup2 to STDOUT failed: $!";
exec 'A.pl' or die "exec failed: $!";
}

close $FROMPARENT;
close $NEWCONN;

Obviously you want to put all this in a sub, so you can say

my $TOCHILD = accept_new_child($LISTENER);

If you get errors from the open '<&' syntax, you may need a newer
version of perl; or you can replace them with

open STDIN, '<&' . fileno $FROMPARENT or ...

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'


All times are GMT. The time now is 07:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57