donaldm314 has asked for the wisdom of the Perl Monks concerning the following question:

I am using RPC.pm, from Advanced Perl Programming, to query an NT database from a linux box. Very cool module!

I have a script that runs on the NT box (server.pl) and another that runs on Linux (client.pl). RPC.pm allows me to call server.pl methods from client.pl, similar to the following.

In server.pl:
sub is_ID_valid {
    my $ID = shift;
    # Query the database, see if the ID is valid.

    return $valid;
}
In client.pl:
$valid = $connection->rpc('is_ID_valid', $ID);
This works quite well. The RPC module dispatches the 'is_ID_valid' from the client to the server to which we are connected.

In Chapter 13 of Advanced Perl Programming, Srinivasan states that, "The client code knows it is making an RPC call. Making this transparent (as typical RPC systems do) is quite simple, really. Using eval, we can dynamically create a dummy client stub ... on the caller's side and have it make the call to rpc(). "

It's that, "quite simple, really," part that has me seeking wisdom.

  1. How do I use eval to create a dummy stub on the client side?
  2. In Srinivasan's example rpcdemo.pl, the client and server code are in a single script. Would this be a better way to go?
  • Comment on RPC.pm and using eval to make a dummy client stub

Replies are listed 'Best First'.
Re: RPC.pm and using eval to make a dummy client stub
by bikeNomad (Priest) on Jun 01, 2001 at 20:57 UTC
    You could do this using an AUTOLOAD:
    # assumes connection is first arg sub AUTOLOAD { (my $name = $AUTOLOAD) =~ s/.*:://; eval "sub $AUTOLOAD { shift()->rpc( '$name', \@_) }"; goto &$AUTOLOAD; }

    update: removed debugging print statement, unnecessary variable.

      I just wanted to remind that one should include a dummy DESTROY subroutine if AUTOLOAD is implemented, because Perl will call AUTOLOAD when the object goes out of scope, very wasteful unless the clean up code will be implemented in the AUTOLOAD method.

      Jeff

      R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
      L-L--L-L--L-L--L-L--L-L--L-L--L-L--
      
(jeffa) Re: RPC.pm and using eval to make a dummy client stub
by jeffa (Bishop) on Jun 01, 2001 at 23:54 UTC
    Per question #2 - depends on the circumstances, for testing purposes, a single script might be easier to maintain, but for any real world use of RPC: the server and client are going to be seperated.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--