in reply to Determining if you have balanced delimiters

sub balanced_delimiters { local $_ = shift; my %sub; @sub{@_} = ("(",")","\\"); my $LRS=join'|',map quotemeta,@_; {local $^W=0;s/($LRS|.)/$sub{$1}/gs; eval{m/$_/} } return !$@; }

Replies are listed 'Best First'.
Re (tilly) 2 (fails): Determining if you have balanced delimiters
by tilly (Archbishop) on Dec 16, 2000 at 22:55 UTC
    Darned clever. Too bad that:
    balanced_delimiters('(\a)', '(', ')', "\\")
    returns the wrong thing. Perhaps you meant this?
    sub balanced_delimiters { local $_ = shift; my %sub; @sub{@_} = ("(",")"); my $LRS=join'|',map quotemeta,@_; $LRS .= "($LRS)"; {local $^W=0;s/($LRS|.)/$sub{$1}/gs; eval{m/$_/} } return !$@; }
    A behaviour note, the escape sequence properly escapes the escape sequence.
      Thank you for catching that, it was meant more like
      @sub{@_} = ("(",")"); my $LRS=join'|',map quotemeta,@_; {local $^W=0;s/($LRS.|.)/$sub{$1}/gs; eval{m/$_/}