Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: open many sockets in script

by ig (Vicar)
on Jul 04, 2012 at 21:02 UTC ( #979920=note: print w/replies, xml ) Need Help??

in reply to open many sockets in script

A my variable used inside the loop is local to the loop: you don't need a different variable name for each iteration through the loop so you can just use my $sock.

If you want to create all the sockets and have them accessible outside the loop, then you should declare the variable outside the loop and set it inside the loop. In this case, if you want to use integers to access the various sockets I suggest you use an array, rather than differently named variables. Something like:

my @sockets; foreach my $i (0..9) { $sockets[$i] = new IO::Socket::INET( PeerAddr => '', PeerPort => '389', Proto => 'tcp', ); die "Could not create socket: $!\n" unless($sockets[$i]); } # Use the sockets here as $sockets[1], $sockets[2], etc.

Replies are listed 'Best First'.
Re^2: open many sockets in script
by httpd (Novice) on Jul 04, 2012 at 22:59 UTC
    thanks guys, you saved me much time :)

      ...and now you're wasting ours by not leaving the question up so the answers make sense.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://979920]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-06-27 12:30 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (604 votes). Check out past polls.