#!/usr/bin/perl -w use strict; my %x = (')'=>'(','}'=>'{',']'=>'['); my @braces = (); while (<>) { s/\\(\(|\)|\{|\}|\[|\])//g; # yank out escaped braces for (split //, $_) { # split each line into chars push(@braces, $_), next if (/\(|\{|\[/); # push if left brace /(\)|\}|\])/ and # if right brace (@braces > 0 # and left braces on stack ? $braces[-1] eq $x{$1} # and right brace matches left ? pop @braces # pop the pair : (print("unmatched '$braces[-1]' before '$_' on line $."), exit) # saw a left without correct right : (print("no match for '$_' on line $."), exit) # saw a right with no stack ); } } print "passed!\n";