On Tue, Jan 27, 2004 at 05:04:03PM -0500, Tolkin, Steve wrote:
> # run using e.g. echo hello | perl this-file
> 
> # Why doesn't perl produce a warning from the following.  It is an
> # infinite loop.  If I add a /g modifier to the m// it works fine.
> 
> while (<>) {
>     while (m/([a-z])/) { # warning infinite loop!!! 
>         print $1, "\n"
>     }
> }
>     
> 
> ///  
> 
> In general it is hard to detect infinite loops, but in this case it is easy,
> because the pattern is a constant.  I think this is a very common
> special case, and is worth detecting.

The pattern in the below code is also constant, but there is no infinite
loop:

while (<>) {
  while (m/([a-z])/) {
    print $1, "\n";
    $_ = substr($_, 1);
  }
}

As you say, it is hard to detect infinite loops.  :)


Ronald
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to