3

I'm working on a script for the VoIP software, Freeswitch. The script will run as an instance listening for inbound messages to a socket.

I used an example script provided with Freeswitch, to start with, and everything works fine.

However, one bit is throwing me off.

use IO::Socket::INET;
use warnings;
use strict;


my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1',  LocalPort => '8060',  Proto => 'tcp',  Listen => 1,  Reuse => 1 );
die "Could not create socket: $!\n" unless $sock;


for(;;) {
    my $new_sock = $sock->accept();
    my $pid = fork();
    if ($pid) {
      close($new_sock);
      next;
    } 
    close($new_sock); 
}

What exactly does the ;; mean? Is it a special operator that is defined when a socket is created? Struggling to find documentation!

Anto
  • 6,806
  • 8
  • 43
  • 65
BIGMOOSE
  • 146
  • 7
  • It just means to loop forever. – Mark Setchell Apr 13 '14 at 18:42
  • `for (;;) { ... }`, `while () { ... }` and , `while (1) { ... }` compile identically into an "inifinite" loop. On the other hand, `{ ... }` compiles into loop that only executes once. You can use `next`, `last` and `redo` with these loops (even `{ ... }`), just like all other loops. – ikegami Apr 13 '14 at 19:11
  • Syntax::Feature::Loop adds `loop { ... }`, which does the same thing. – ikegami Apr 13 '14 at 19:16
  • https://stackoverflow.com/questions/2737550/what-does-for-mean-in-perl – isomorphismes Jul 31 '17 at 16:34

3 Answers3

8

for(;;) is a C-ish idiom which is read as for ever, and is a loop that never terminates. People who don't come from C would write while (1) instead.

Normally, this C-style for loop has three statements: initialization, looping condition, and some step:

for (init; condition; step) {
    body;
}

which is exactly equivalent to

{
    init;
    while (condition) {
        body;
    }
    continue {
        step;
    }
}

An empty condition is taken to be always true.

A common usage would be to iterate over all numbers in a range:

for (my $i = 0; $i < @array; $i++) {
    say $array[$i];
}

However, this would be written more idiomatically with a foreach loop:

for my $i (0 .. $#array) {
    say $array[$i];
}

or

for my $item (@array) {
    say $item;
}

which is why you see the C-style for loop very rarely in idiomatic Perl code.

This construct is documented in perldoc perlsyn.

friedo
  • 65,762
  • 16
  • 114
  • 184
amon
  • 57,091
  • 2
  • 89
  • 149
1

It's just an infinite loop, equivalent to writing while(1).

It works because Perl has a C-style for loop with three parameters: an initialization, a condition, and an expression to execute on each loop.

Normally, this would be something like for(x=0; x<10; x++) meaning "initialize x to 0, test if x is less than 10, and increment x each time around the loop".

The trick is that all three are optional, so for(;;) means "initialize nothing, test nothing, and do nothing special at the end of the loop".

IMSoP
  • 89,526
  • 13
  • 117
  • 169
0

its just an infinite loop, as in case of C programming language. Your code will get stuck at this statement for eternity .. or in your case, execute the for block till the program is manually terminated.