Welcome to the Monastery PerlMonks

### How many loops are there in Perl?

by shajiindia (Acolyte)
 on Jan 31, 2012 at 06:40 UTC Need Help??
shajiindia has asked for the wisdom of the Perl Monks concerning the following question:

I was asked a question "How many loops are there in Perl w.r.t to version 5.14.2 and from my knowledge I said 7. Am I right? or is there something I am missing?
```#
# while loop
#

\$i = 0;
while (\$i < 3) {
print "Inside while loop!\n";
\$i++;
}

#
# until loop
#

\$i = 0;
until (\$i > 3) {
print "Inside until loop!\n";
\$i++;
}

#
# do while loop
#

\$i = 0;
do {
print "Inside do while!\n";
\$i++;
} while (\$i < 3);

#
# do until loop
#

\$i = 0;
do {
print "Inside do until!\n";
\$i++;
} until (\$i > 3);

#
# for loop
#

for(\$i = 0;\$i < 3;\$i++) {
print "Inside for loop!\n";
}

#
# foreach loop
#

foreach (1..3) {
print "Inside foreach!\n";
}

#
# naked block
#

{
\$i = 0;
print "Inside naked block\n";
\$i++;
}

Replies are listed 'Best First'.
Re: How many loops are there in Perl?
by JavaFan (Canon) on Jan 31, 2012 at 07:29 UTC
```\$i = 10;L: print "Hello"; goto L if --\$_i;

map {print "Hello"} 1 .. 10;

grep {print "Hello"} 1 .. 10;

\$_ = "abcdefghij"; s/./@{[print "Hello"]}/g;

sub foo {
print "Hello";
foo(\$_[0]-1) if \$_[0] > 0;
}
foo(10);

In the spirit of your map and grep examples, here's one using sort...

```perl -E'join("|", sort {do{say "Inside sort loop"} and \$a <=> \$b} 3,1,
+2)'

Works in 5.10.1, but the Perl sorting algorithm has been known to change from version to version. Note that the join does seem to be needed to avoid Perl optimizing away the sort.

Re: How many loops are there in Perl?
by tobyink (Abbot) on Jan 31, 2012 at 07:21 UTC

Dunno... does this count?

```my (\$from, \$to) = (1, 3);
my \$x; \$x = sub {
my \$i = shift;
print "Inside recursive sub!\n";
\$x->(++\$i) if \$i < \$to;
} and \$x->(\$from);

TIMT7WTDI

Re: How many loops are there in Perl?
by repellent (Priest) on Jan 31, 2012 at 08:32 UTC
```[1 .. 5] ~~ sub { say "Inside loop \$_[0]?" };  # Perl 5.10.1+

That's one for the obfu-toolkit. I'm surprised we haven't seen abuse of the regex engine and (?{...}) blocks.

Dave

as you wish :)

```  DB<72> (" "x10) =~ m/ (?{print \$x++})/g
=> (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ")
0123456789

I'm surprised nobody tried s///e

Cheers Rolf

update:

```  DB<20> \$_=" "x10; s/ /print \$x++/ge
=> 10
0123456789
Re: How many loops are there in Perl?
by Anonymous Monk on Jan 31, 2012 at 06:45 UTC

Am I right?

The question is vague.

I suppose the naked-block isn't a loop, at least not without a label and goto

Naked blocks can be loops with no need for goto or labels:

```my \$i = 0;
{
redo if \$i++ < 10;
}

But as worded, the question is difficult to answer; it's hard to know what the person asking is really after and what he or she has or hasn't considered.

Dave

... the naked-block isn't a loop ... without a label and goto[.]

```perl -wMstrict -le
"my \$i = 1;  { print qq{in naked loop \$i};  redo if \$i++ < 3; }
"
in naked loop 1
in naked loop 2
in naked loop 3
I have always thought redo was cool. At first glance it doesn't look anything like a goto because it is usually used with bare blocks. The code below has redo going to the start of the nearest enclosing block.
```{
print "Enter a number 0-9\n";
\$number=<STDIN>;
chomp(\$number);
unless(length(\$number) == 1  && \$number =~ /[0-9]/){
print "C'mon, man! give me a number 0-9!\n";
redo;
}
}
However, redo can take an argument (a block label) which makes it look much more goto-ish and less like elegant Perl. For example, we could redo (pun intended!) the example like this:
```GET_NUMBER:{
print "Enter a number 0-9\n";
\$number=<STDIN>;
chomp(\$number);
unless(length(\$number) == 1  && \$number =~ /[0-9]/){
print "C'mon, man! give me a number 0-9!\n";
redo GET_NUMBER;
}
}
The biggest difference between redo and a pure goto is that redo must go to an enclosing block label whereas goto goes to any label. This is still a valid redo:
```my \$number;

PROMPT_FOR_NUMBER:{
print "Enter a number 0-9\n";
GET_NUMBER:{
\$number=<STDIN>;
chomp(\$number);
unless(length(\$number) ==1  && \$number =~ /[0-9]/){
print "You don't want to give me one number ?\n";
redo PROMPT_FOR_NUMBER;
}
}
}
<jc> Why do people persist in asking me stupid questions?
<Petruchio> <insert mutually recursive response>
--an exchange from #perlmonks on irc.slashnet.org(2 March 2009 1345 EST)
Also, by OPs criteria,  do { } for and  do { } foreach (with or without the do block) could be considered four more loops

Actualy question is not vague but naked braces is not a loop. SO whatever you have said 6 loops that is fine.

Yes, the question as asked is extremely vague, as evidenced by the things offered as loops

map/grep/foreach cannot loop forever, they are iterators, but are still considered loops by some

```\$ perl -le " print for 1 .. INF "
Range iterator outside integer range at -e line 1.
Re: How many loops are there in Perl?
by cursion (Pilgrim) on Jan 31, 2012 at 14:04 UTC
```print something() x10;
EDIT: On second thought (see reply to this) .. this only applies to a very loose definition (aka incorrect) of a loop.
not a loop because something() is only executed once and not 10 times.

Cheers Rolf

Re: How many loops are there in Perl?
by jdrago999 (Pilgrim) on Jan 31, 2012 at 18:30 UTC

I'd like to see how a hyperoperator could be used to simulate a loop.

Re: How many loops are there in Perl?
by rir (Vicar) on Feb 01, 2012 at 21:50 UTC
One or two, depends on whether you're willing to accept it capitalized. Cursive is not considered.

Perl6 goes one better.

Be well,
rir

Create A New User
Node Status?
node history
Node Type: perlquestion [id://950889]
Approved by Corion
Front-paged by Old_Gray_Bear
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-06-23 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (125 votes). Check out past polls.

Notices?