[Purdue-pm] while(1) "trick"

Mark Senn mark at purdue.edu
Mon Jul 28 07:03:00 PDT 2014


Some times I like to use a "while(1)" only so I can do a "and last" to break
out of the while loop.  For quick and dirty code I find it easier than using
given/when, ifs, setting up arrays or hashes of regular expressions and
replacement text, etc.

Here is a contrived example:

    #!/usr/local/bin/perl

    while (<STDIN>)
    {
        chomp;
        study;
        while (1)
        {
            s/blue.*$/green/o   and  last;
            s/apple/banana/o    and  last;
            s/[fF]red/Barney/o  and  last;
            # a gazillion more s///o commands here
            die "nothing matched";
        }
        print qq/output is "$_"\n/;
    }

Thought you might be interested.

-mark


More information about the Purdue-pm mailing list