Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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.

Comment on Re: open many sockets in script
Select or Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2015-11-29 11:27 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (750 votes), past polls