On 8/24/11 Wed  Aug 24, 2011  8:32 AM, "Emeka" <emekami...@gmail.com>
scribbled:

> Do we really need "goto" here?
> 
> Emeka

No, we don't need a goto here. The statement 'last' is effectively a 'goto'
but with the restriction that it will only work inside a loop and only
transfer control out of the loop. This avoids many of the drawbacks of a
more general goto statement, which can be used anywhere and transfer control
to anywhere.
 
> 
> On Mon, Aug 22, 2011 at 10:03 AM, Shlomi Fish <shlo...@shlomifish.org>wrote:
> 
>> Hi Alan,
>> 
>> On Mon, 22 Aug 2011 13:10:05 +0530
>> Alan Haggai Alavi <alanhag...@alanhaggai.org> wrote:
>> 
>>> Hello Anant,
>>> 
>>>> i want to input some numbers via <stdin> in while loop.And loop should
>> be
>>>> broken if any nonnumeric character is entered.So how it can be checked.
>>>> 

>> 
>> It's a good idea to always use "last LABEL;" instead of "last;" (as well as
>> "next LABEL;" etc. in case more loops are added in between.
>> 
>> So the program becomes:
>> 
>> [CODE]
>> use strict;
>> use warnings;
>> 
>> my @numbers;
>> print "Enter numbers:\n";
>> STDIN_LOOP:
>> while (1) {
>>    chomp( my $number = <STDIN> );
>>    if ( $number =~ /\D/ ) {
>>        print "$number is not a numeric value.\n";
>>         last STDIN_LOOP;
>>    }
>>    else {
>>        push @numbers, $number;
>>    }
>> }
>> [/CODE]



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to