Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > File handlings

Reply
Thread Tools

File handlings

 
 
Glodalec
Guest
Posts: n/a
 
      02-03-2004
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
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      02-03-2004

Glodalec <(E-Mail Removed)> 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] * http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Glodalec
Guest
Posts: n/a
 
      02-03-2004
In article <bvoe9t$t31$(E-Mail Removed)>,
(E-Mail Removed) says...
>
> Glodalec <(E-Mail Removed)> 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
>
>

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-03-2004

Glodalec <(E-Mail Removed)> 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.
~ (E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting JPG file ppt file [Powerpoint] file zxcvar Digital Photography 7 06-22-2009 07:54 PM
How to create MDF file (.mdf) file from XML file. Dave ASP .Net 1 06-07-2007 11:32 PM
Applets and File handlings AnetaKvel Java 1 01-06-2007 04:16 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM



Advertisments