develooper Front page | perl.perl5.porters | Postings from August 2013

Re: [perl #116020] toggle operator missing reset: proposal

Thread Previous | Thread Next
From:
Brian Fraser
Date:
August 29, 2013 19:09
Subject:
Re: [perl #116020] toggle operator missing reset: proposal
Message ID:
CA+nL+nYmxGEEo321PaktWHYuQ4Fywi=aux3as18qqte9Gy3JbQ@mail.gmail.com
On Thu, Aug 29, 2013 at 12:48 PM, Zefram <zefram@fysh.org> wrote:

> tlhackque wrote:
> >>One might imagine a toggle object as a middle ground -- e.g.
> >>    $toggle = Toggle::new( '...', sub {/^--a$/} , sub {/^--b$/}); #
>
> Can be done much more neatly by a call-checker plugin and custom ops.
> I'm thinking of it looking something like
>
>         $out .= $_ if toggle /^--a$/, /^--b$/;
>

Heh. I was about to do a bit of shameless advertisement by saying that "you
can do that with Params::Lazy right now!", but turn out, you can't without
some silly workarounds, at least until I push this new version to CPAN :P
But well, this works on the dev version:

sub toggle;
use Params::Lazy toggle => q(^^);

my $toggle_state = '';
sub toggle {
    my ($begin, $end) = @_;

    if ( !$toggle_state ) {
        $toggle_state = 1 if force $begin;
    }
    else {
        if ( force $end ) {
            $toggle_state = '';
            return "1E0";
        }
        else {
            $toggle_state++;
        }
    }

    return $toggle_state;
}

my $out;
for (qw(no1 no2 --a first second third --b no3)) {
    $out .= "<$_>" if toggle /^--a$/, /^--b$/;
}

say $out;


> Named toggles just need a slight extension to put their state somewhere
> visible.  As the hiddenness of a toggle's state is a nasty aspect of
> the core feature, I'd be inclined to make these toggles use an explicit
> variable, whose lifetime can be controlled in the usual ways:
>
>         my $tog;
>         reset_toggle $tog;
>         $out .= $_ if toggle $tog, /^--a$/, /^--b$/;
>

I believe this would "just" require extending the code above to also use
the * prototype.


>
> >>such as
> >>
> >>      reset ['label']; or reset( ['label'], 2 );
>
> If a toggle reset feature is added to the core, it should not further
> overload an existing core operator, especially one that is presently
> unrelated.
>
>
+1

Thread Previous | Thread Next


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