Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How many loops are there in Perl?

by Anonymous Monk
on Jan 31, 2012 at 06:45 UTC ( #950892=note: print w/ replies, xml ) Need Help??


in reply to How many loops are there in Perl?

Am I right?

The question is vague.

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


Comment on Re: How many loops are there in Perl?
Re^2: How many loops are there in Perl?
by Anonymous Monk on Jan 31, 2012 at 06:49 UTC
    Also, by OPs criteria,  do { } for and  do { } foreach (with or without the do block) could be considered four more loops
Re^2: How many loops are there in Perl?
by AnomalousMonk (Monsignor) on Jan 31, 2012 at 07:03 UTC
    ... the naked-block isn't a loop ... without a label and goto[.]

    Or at least a (possibly conditional) redo (see also Loop Control):

    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)
Re^2: How many loops are there in Perl?
by davido (Archbishop) on Jan 31, 2012 at 07:11 UTC

    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

Re^2: How many loops are there in Perl?
by bimleshsharma (Beadle) on Jan 31, 2012 at 09:59 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950892]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-08-28 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls