Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2016-10-27 20:24 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (371 votes). Check out past polls.