in reply to Re: globbing,file handles and subroutines
in thread globbing,file handles and subroutines

I'm not really sure what you're suggesting. I'm using IO::Socket to create the socket. Now I want to draw data from it by calling a function. Here is how it's created:
$sock = new IO::Socket::INET( LocalHost => $myip, LocalPort => $port, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1); $sock or die "no socket :$!"; STDOUT->autoflush(1); #main loop begin { while (($new_sock, $c_addr) = $sock->accept()) { ($client_port, $c_ip) = sockaddr_in($c_addr); $client_ipnum = inet_ntoa($c_ip); $client_host = gethostbyaddr($c_ip, AF_INET); next if $kid = fork; die "fork: $!" unless defined $kid; close $sock; if (&readfromsocket(*new_sock)){
In the subroutine I could do this:
if (defined($lineread = <$new_sock>)) {whatever}
But I want to be able to use another var name in the subroutine.

Replies are listed 'Best First'.
Re: Re: Re: globbing,file handles and subroutines
by l2kashe (Deacon) on Mar 15, 2003 at 04:08 UTC
    You can simply pass the $sock to the subroutine and my it.. Its already a ref to a filehandle, hence you can work with it just like you are proposing... Here is a simple snippet to test sendmail on the local machine...
    use strict; use IO::Socket; my $sock = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => '25', Proto => 'tcp', Type => SOCK_STREAM ); do_mail($sock); print $sock "quit\n"; close($sock); sub do_mail { my($handle) = shift; my $buf = <$handle>; print "Sendmail ", $buf ? "is OK\n" : "is not responding\n"; }
    See? no alterations need to be made and the handles name is local to the sub routine.. the actual handle is still global I believe in this case, so I wouldnt suggest trying to have multiple sub routines reading the same handle at the same time..


    /* And the Creator, against his better judgement, wrote man.c */
      Yup. That works. I was offtrack thinking I had to do something really special...

      Thanks!
Re: Re: Re: globbing,file handles and subroutines
by drake50 (Pilgrim) on Mar 15, 2003 at 02:57 UTC
    Here is a little more info. I added the following code inside the subroutine:
    my $socketname = shift; print "\$new_sock=$new_sock\n"; print "\$socketname=$socketname\n";
    And got this output:
    $new_sock=IO::Socket::INET=GLOB(0x8130a94)
    $socketname=*main::new_sock
    
    But I still don't know enough about this stuff to know what I'm doing wrong.