On Wed, May 22, 2002 at 11:11:13PM +0100, [EMAIL PROTECTED] 
wrote:
> Scot. Is $a guaranteed to be untouched?

Perhaps some context would help (and possibly present alternative
solutions, though these have been really fun).

Imagine something like this:

    ## $a is a flag that indicates we should do something
    $SIG{HUP} = sub { $a++ };
    
    while( 1 ) {
        foo();
        ...
        bar();
        ...
    
        ## check to see if we need to reread config file
        if( data_is_stale() || $a ) {
            $a = 0;
            reread_config_file();
            do_something_else();
            do_yet_something_else();
        }
    
        ## now $a is 0 and the above code won't get invoked again until
        ## someone sends us a HUP
    }

For simplicity's sake, I reduced the problem to what Alistair quoted
above:

    if( $a ) {
        $a = 0;
        do_something();
        do_something_else();
        ...
    }

Hence my original question: can the 'if( $a ) { $a = 0; ... }'
construct be done in fewer characters than my best try:

    if( $a%2 .. $a-- ) {
        do_something();
        ...
    }

My example assumes the following constraints: $a is zero (or any even
number) to begin with. This next example doesn't assume digits, but it
does require the starting value of $a to be the empty string:

    $SIG{HUP} = sub { $a='foo' };
    $a = '';

    while( 1 ) {
        yada_yada();

        if( $a=~s/.*// ) {
            do_something();
            ...
        }
    }

I don't care what constraints you impose, as long as the block gets
executed exactly once and only when $a is true (for whatever you want
to define as true).

Shortest (looking) answer yet (which several people posted) is:

    $a && $a-- && do {
        do_something();
       ...
    }

Two questions: a) can it be shortened to fewer characters? and b) is
there another way to do it better in general?

This has been a fun thread, btw (for me, anyway). Thanks.

Scott
-- 
Scott Wiersdorf
[EMAIL PROTECTED]

Reply via email to