in reply to Doing "it" only once

#!/usr/bin/perl use strict; use warnings; my @labels = qw /L0 L1 L2/; while (<DATA>) { chomp; goto $labels[0]; L0: if (/foo/) { print "FOO!\n"; @labels = grep {$_ ne "L0"} @labels; next; } goto $labels[1]; L1: if (/bar/) { print "BAR!\n"; @labels = grep {$_ ne "L1"} @labels; next; } L2: print "$_ "; } print "\n"; __DATA__ one two bar three four foo five six foo seven eight bar nine ten one two BAR! three four FOO! five six foo seven eight bar nine ten

Replies are listed 'Best First'.
Re^2: Doing "it" only once
by QM (Parson) on Sep 22, 2005 at 18:39 UTC
    my @labels = qw /L0 L1 L2/; ... goto $labels[0]; L0:
    Hard to code and maintain. You want a construct that works correctly without depending on you getting all of the labels correct, or selecting the proper index in the labels array. Your code has 3 places that have to "match" to function.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      You want a construct that works correctly without depending on you getting all of the labels correct,

      Sure, once you start writing code that works correctly without depending you getting all the subroutine names correct.

      selecting the proper index in the labels array.

      Well, it's not very hard to count 0, 1, 2, .... ;-)