Ang-st has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks
i try to call method to print to a handle (a socket in this case...)
i have defined a package looking like this :
{ package Fake; sub present { my $class = shift ; print $class->head, "\n"; } sub ack { my $class = shift; print $class->ok,"\n"; } sub rst { my $class = shift; print $class->quit,"\n"; } } { package Ssh; @ISA = qw{Fake}; sub head { my @fake = ("SSH-2.0-OpenSSH_3.8.1p1","SSH-2.0-OpenSSH_3.7p1", +"SSH-2.0-OpenSSH_3.5p1"); $size = @fake; $rand = 0; $rand = int(rand $size); return $fake[$rand]; } sub quit {return "Protocol mismatch."} }

the fact is i would create a lot of socket with different (fake)protocol so call a subroutine like
bind($port, 'Ssh'); sub bind ( $$){ my $port = shift; my $obj =shift ...... }
but when try to print to the socket with for example print $socket "$obj->present" i get "Ssh->present" on the client screen or if i try print $socket $obj->present i get the good message but on STDOUT ...
I don't find a way to print to the right filehandle, maybe you can see where is my mistake
Thank's in advance

Replies are listed 'Best First'.
Re: call a method which print to a handle
by dave_the_m (Monsignor) on Mar 23, 2005 at 22:13 UTC
    if i try print $socket $obj->present i get the good message but on STDOUT
    Your present method should just return the data rather than printing it out itself, ie both present() and the thing calling it are trying to do a print.

    Dave.

      Thanks a lot Dave :-)
Re: call a method which print to a handle
by holli (Abbot) on Mar 24, 2005 at 05:38 UTC
    or pass the handle to your methods:
    sub present { my $class = shift; my $socket = shift; print $socket $class->head, "\n"; } ... $obj->present ($socket);


    holli, /regexed monk/