- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: perl file handles refs
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2005 10:30 PM
01-19-2005 10:30 PM
perl file handles refs
I can't achive something in perl and I can't find any solution anywhere... but I know it can be done.
I am writing a package. In this package a function is passed 2 args : the "read" part of a pipe, and the "write" part of another pipe. Those pipes are reopened as STDIN and STDOUT. function forks and do its job.
Problem is that I can't seem to be able to reopen those pipes.
Here is a test implementation of what I try to do. It has been simplified to let only the problematic part.
=====
# tst.pm
package tst;
use strict;
no strict "refs";
sub new {
my $class=shift;
my $self={};
$self->{pipe2Write}=$_[0];
bless($self,$class);
return $self;
}
sub hello {
my $self=shift;
my $message=join("",@_);
my $pipeHandlerRef=$self->{pipe2Write};
my $pipeHandler=$$pipeHandlerRef;
open STDOUT,">",$pipeHandler;
print $message,"\n";
# Comment the 2 preceding line and uncomment
# the following works great
# print $pipeHandler $message,"\n";
return 0;
}
return 1;
=====
#hello.pl
#!/opt/perl/bin/perl
use strict;
use tst;
use IO::Handle;
my $readPipe;
my $writePipe;
pipe $readPipe,$writePipe;
$readPipe->autoflush(1);
$writePipe->autoflush(1);
my $myTst=tst->new(\$writePipe);
$myTst->hello("Hello");
$myTst->hello("World");
$myTst->hello("!!!");
close $writePipe;
while (<$readPipe>) {
print "READ from pipe : $_";
}
close $readPipe;
=====
So, what's the problem (may be a missing & or something like that)
Regards,
Fred
"Reality is just a point of view." (P. K. D.)
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 12:22 AM
01-20-2005 12:22 AM
Re: perl file handles refs
The "readPipe" (filehandle=4) and "writePipe" (filehandle=7) have nothing to do with STDIN or STDOUT, they are simply a PAIR of related read/write pipes:
main::(./hello.pl:11): $readPipe->autoflush(1);
DB<1> x $readPipe
0 GLOB(0x403f32e4)
->
FileHandle({*main::$readPipe}) => fileno(4)
DB<2> x $writePipe
0 GLOB(0x4001eaec)
->
FileHandle({*main::$writePipe}) => fileno(7)
DB<3>
I used "use Data::Dumper;" and the -d option to get into debug mode.
live free or die
harry d brown jr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 12:39 AM
01-20-2005 12:39 AM
Re: perl file handles refs
I thought pipes were treated as files. I can use them as "print myFifo ..." or "$a=
Isn't there a way to implement what I want ?
Regards,
Fred
"Reality is just a point of view." (P. K. D.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 12:53 AM
01-20-2005 12:53 AM
Re: perl file handles refs
Also, have you "man perlipc" ?
live free or die
harry d brown jr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 01:08 AM
01-20-2005 01:08 AM
Re: perl file handles refs
I know about those problems, but I wanted to communicate with a background process (send commands, read output) and pipes seemed to be the right way.
Regards,
Fred
"Reality is just a point of view." (P. K. D.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 01:16 AM
01-20-2005 01:16 AM
Re: perl file handles refs
From perldoc perlipc
Safe Pipe Opens
Another interesting approach to IPC is making your single program go multiprocess and communicate between (or even amongst) yourselves. The open() function will accept a file argument of either "-|" or "|-" to do a very interesting thing: it forks a child connected to the filehandle you've opened. The child is running the same program as the parent. This is useful for safely opening a file when running under an assumed UID or GID, for example. If you open a pipe to minus, you can write to the filehandle you opened and your kid will find it in his STDIN. If you open a pipe from minus, you can read from the filehandle you opened whatever your kid writes to his STDOUT.
live free or die
harry d brown jr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 01:27 AM
01-20-2005 01:27 AM
Re: perl file handles refs
Fred,
If you want to "talk" to a background process, say one that is continously running (say some daemon process), you can use sockets or named pipes to "talk" to it.
live free or die
harry d brown jr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 02:42 AM
01-20-2005 02:42 AM
Re: perl file handles refs
I think I will go for named pipes, but it doesn't seem as transparent than the pipe command to me.
Regards,
Fred
"Reality is just a point of view." (P. K. D.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2005 03:28 AM
01-20-2005 03:28 AM
Re: perl file handles refs
If you open a pipe from minus, you can read from the filehandle you opened whatever your kid (child) writes to his STDOUT.
live free or die
harry d brown jr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2005 12:21 AM
01-21-2005 12:21 AM
Re: perl file handles refs
father sends commands to child,
child sends result to father.
So, |- or -| can't do as it runs a one-way communication.
I have now implemented with named pipes. It requires more, cause I need to check for uniqueness of pipe's names (multiple instances of my object may be running). So if anybody has any idea to go with unnamed pipes...
Thanks Harry for your time and good advices,
Fred
"Reality is just a point of view." (P. K. D.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2005 02:06 AM
01-21-2005 02:06 AM
Re: perl file handles refs
But didn't you go as far down as where the sections on bidirectional pipes start?
There they also mention the function socketpair() (perldoc -f socketpair).
As for passing file handles.
They are usually passed as either globs or globrefs to subs.
There in the subs they are assigned to scalars and are used straight forward, afaik.
See perldoc perlsub and the section "Pass by Reference"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2005 02:15 AM
01-21-2005 02:15 AM
Re: perl file handles refs
Try passing globs or globrefs.
Afaik globs live in the (global) symbol table while lexical scalars don't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2005 02:20 AM
01-21-2005 02:20 AM
Re: perl file handles refs
our ($readPipe, $writePipe)
or
use vars qw($readPipe $writePipe)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2005 07:17 AM
01-24-2005 07:17 AM
Re: perl file handles refs
Does the following FAQ helps ?
perldoc -q pipe
"How can I open a pipe both to and from a command?"
__Vali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 07:46 PM
01-25-2005 07:46 PM
Re: perl file handles refs
Regards,
Fred
"Reality is just a point of view." (P. K. D.)