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

I have 2 programs: 1 client, 1 server. (simple, right?). I came across the RPC and MSG modules written in Advanced Perl Programming and put them to use. I have written a routine that looks like:
sub checkuser { my $obj = shift; my $FN = foo::get_attr($obj , 'FIRST_NAME'); my $LN = foo::get_attr($obj , 'LAST_NAME'); my $USERS; my @USERS; my $line = ""; open( 'USERS' , 'file' ); while ( $line = <USERS> ) { chomp($line); my @parser = (); my $item = ""; @parser = split( /,/ , $line); foreach $item (@parser) { $item =~ s/^\s+//; $item =~ s/\s+$//; } push (@USERS, \@parser); $USERS = @USERS; } close('USERS'); my $i = 0; my $return_result; while ( $i < $USERS ) { my $FNT = $USERS[$i][1]; my $LNT = $USERS[$i][0]; if ( ( $FN =~ /\Q$FNT/i) && ( $LN =~ /\Q$LNT/i) ) { $return_result = 1; return ($USERS[$i]->[0] , $USERS[$i]->[1]); } else { $return_result = "User not found"; } $i++; } return $return_result; }
I had this code reviewed by others as well as reviewing it myself. It works fine when running on the client (the above sub is server code) as part of the client (i.e., inserted in the client program). When I put the server code back on the server, then call the command via RPC ($client->rpc('checkuser' , $obj), which is the client calling the sub from the server), the client hangs. I figure I am missing something very simple here. I've looked at the modules, and from what I gather, I am using it properly. The only thing that would make sense to me is that I am passing a blessed hash (which should not matter), $obj to the checkuser sub. Any help will be appreciated... Necos

Replies are listed 'Best First'.
(tye)Re: RPC
by tye (Sage) on May 05, 2001 at 07:53 UTC

    Well, there are a few cases left to try, don't you think? Run the code via RPC within the client machine. On the server, stub out the client bits and run the subroutine w/o RPC.

    Not being shy about making wild guesses in public, I suspect that the second case will turn up a problem. For example, are you sure what the current working directory is when the sub is called via RPC? I'll even guess that your open is failing, causing the <USERS> line to die which probably means that RPC won't be responding anymore.

    Oh, and fire your code reviewers if they didn't tell you to check for failure of open! (:

    P.S. The quotes around 'USERS' are just fine.

            - tye (but my friends call me "Tye")
      Heya Tye. The sub I wrote was initially tested with die during the first coding session. Unfortunately, I'm the only coder here that knows Perl. Perl is also the only language I know (it's probably the easiest language since FORTRAN and VB). I was not expecting to run into this problem, so I had avoided large error handling subs. I guess now I must put them in to ensure these programs work properly. I guess I'll have to give up a little bit of the speed now. Oh well, I think the programs will work fine with heavy error handlers (I hope!!!). Thanks for pointing out the cwd possibility. I'll look into that later today. Thanks your your help guys. Any other pointers, style advise, or what-have-you will be much appreciated. Necos

        or die "..." is not "large error handling" and does not make your code run any slower. I suppose you could argue that it will make your code take a few milliseconds longer to compile. If you eventually compile it enough times to make up for the time you've now spent trying to track down this problem, especially when you consider how much you get paid per hour vs. how much your computer costs per hour, then let us know. We'll all be very surprised. (:

                - tye (but my friends call me "Tye")
Re: RPC
by converter (Priest) on May 05, 2001 at 05:18 UTC
    Just a hunch (my hunch was wrong, see Update below), but try removing the single quotes from the filehandles in your open() and close() statements.

    Change this:

    open( 'USERS' , 'file' );
    To this:
    open( USERS , 'file' );
    and while we're at it, let's check the results of the open:
    open(USERS, 'file') or die "unable to open `file': $!";

    Update: my hat's off to tye on the use of a string literal for a filehandle. How many times have I glossed over the following in perlfunc and not noticed?

    If FILEHANDLE is an expression, its value is used as the name of the real filehandle wanted.
    I suppose what threw me is that I've seen no examples of literal strings used to specify filehandles. Live and learn.
      That claim notwithstanding, try this:
      #!/usr/bin/perl -w use strict; my $fh = 'OUT'; open ($fh, "< $0") or die "Cannot read $0: $!"; print <$fh>;
      Try replacing some of the $fh's with 'OUT'. Some combinations will work, some will fail. The list that works/fails changes from 5.005 to 5.6.

      Caveat programmer.