develooper Front page | perl.perl6.language | Postings from November 2002

Re: Control Structures III: flow modifiers

Thread Previous
From:
Luke Palmer
Date:
November 14, 2002 11:02
Subject:
Re: Control Structures III: flow modifiers
Message ID:
20021114190250.96F5E300@babylonia.flatirons.org
> Mailing-List: contact perl6-language-help@perl.org; run by ezmlm
> Date: Fri, 15 Nov 2002 07:46:21 +1100 (EST)
> From: "Timothy S. Nelson" <wayland@smartchat.net.au>
> Sender: wayland@elphin.nelson.org.au
> X-SMTPD: qpsmtpd/0.12, http://develooper.com/code/qpsmtpd/
> 
> 	These are mostly not my ideas (except activate); hopefully not too 
> many of them have already been used.  
> 
> 	In the same list as "last", "next", and "redo", we should also have
> -	deeper (works with nest -- cf. II: loop)
> -	yield and resume (for co-routines)
> 
> 	Also useful could be:
> activate <blockname>
> deactivate <blockname>
> 
> 	Useful for optimising:
> ---------------------------------------
> $first = 1;
> foreach (@example) {
> 	if($first) {
> 		do_something;
> 		$first = 0;
> 	} else {
> 		do_normal_stuff;
> 	}
> }
> ---------------------------------------
> 
> Can be done as:
> 
> ---------------------------------------
> 
> LABEL: foreach(@example) {
> 	OTHEREXAMPLE: {
> 		do_something;
> 		deactivate OTHEREXAMPLE;
> 		next LABEL;
> 	}
> 	do_normal_stuff;
> }
> 
> ---------------------------------------

Also, it could be done as:

    for @example {   # the foreach keyword has gone away
	FIRST {
	    do_something;
	    next;
	}
	do_normal_stuff;
    }

Beautiful.

Deactivating blocks, on a more general scale (if you find it useful),
can be achieved because of the new concept of "everything is a
closure":

    our bit %inactive;
    sub block ($name: &block) {
        &block() unless %inactive{$name}
    }
    sub deactivate ($name) {
        %inactive{$name} = 1
    }

    LABEL: foreach(@example) {
        block 'OTHEREXAMPLE': {
            do_something;
            deactivate 'OTHEREXAMPLE';
            next LABEL;
        }
        do_normal_stuff;
    }

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About