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 */

In reply to Re: Re: Re: globbing,file handles and subroutines by l2kashe
in thread globbing,file handles and subroutines by drake50

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.