[pm-h] Binding variables to a reference

Todd Rinaldo toddr at null.net
Sun Dec 21 11:55:07 PST 2008


> sub baz {
>   for my $baz ( $_[0] )
>   {
>       $baz = 'baz';
>   }
> }
Ok, so short answer is there's no way to create an alias without a sub
or a for loop? Which brings me to my real problem: forks::shared
(http://tinyurl.com/sharedforks).

I don't normally like puking the whole problem, but I'm half way down
the rabbit hole on this problem. I might as well go all the way...

What I'm trying to figure out: How do I use forks::shared to share
variables between parent/child without an anonymous subroutine on the
fork call.

My design issue: I need to create a program that can quickly run
commands on a remote host via SSH. I am using Net::SSH::Perl because
all of the other options are buggy or abandoned. So what I was
thinking was a POE server that takes requests for running a command.
If it already has a SSH object open, it uses that to run the command.
If not, then it creates a new object to handle the connection. The
problem is that Net::SSH::Perl does not destroy properly, which means
your program will slowly leak and blow up if left to run too long. I'm
also unsure how many open connections a single process can stably
handle.

So what I'm thinking is that I can use POE to to take requests for
commands and then it can maintain a list of connections it could send
those commands to. It would have to fork and maintain the connections
in separate processes to prevent the core program from crashing. So if
I want each fork to do more than one command, I have to create a way
to send requests to run commands on the child process and get back the
output. Enter shared variables.

The way forks and threads (the Perl modules) work is that you tell it:
Thread->create(\&sub_to_call, @variables_to_pass). You can share
variables by pre-declaring the variables as shared and then passing
them to the target sub. The sub can then use locking, etc to alter the
variable and the parent will see the changes. I can use this method to
send calls and get back the resulting command. The problem is that
short of using an anonymous sub, I don't see a way to pass in the
shared variable to the target sub the way forks::shared is designed.
You can't pass references to the child you have to pass the variable
it's self.

I realize I've kinda dumped here I thought it might avoid multiple
email passes with me explaining to you that I was only giving you a
stub, not the whole problem and your solution you suggest won't work
for me.

Any suggestions?


More information about the Houston mailing list