|Syntactic Confectionery Delight|
Yeah, I have a few comments, presented here in decreasing order of importance.
Use lexical variables to the extent possible. And secondarily to that, declare lexical variables in the nearest enclosing scope as possible. In most cases that will be the innermost scope in which the variable is first used, but occasionally you need it to survive that scope, so moving the declaration out the necessary number of levels is appropriate.
Avoid "useless use of quotes", aka useless string interpolation. Note that "$foo" evaluates to exactly the same thing as $foo (as long as $foo is already a plain string variable).
I'd recommend using Getopt::Std, rather than hand-rolling a cmdline option parser.
Your die statements for file open errors should include the name of the file, and, ideally, what operation was being attempted and failed.
I recommend using $_ whenever possible, as long as doing so doesn't substantially increase the obfuscation factor. For example, I'd write
These are the situations for which $_ was invented!
It's good style always to use a file open mode indicator. It's true that open F, $foo opens the file for reading by default, but, stylistically, open F, "< $foo" (or open F, "<", $foo) is better. (It is said that that last form, the "three-argument open", is best of all, as it prevents a certain class of bugs.)
Some of your parentheses are unnecessary and add to the clutter (at least in some people's opinion). For example,
could be written as
You might consider using the "ternary operator". It would significantly clean up certain situations, such as this one:
That could be written as
I'm not sure, but it looks like you're just parsing LISP-like structures, with balanced parentheses. If so, you're in serious wheel reinvention territory. It's fine for learning, but for production you might want to consider using a module such as Text::Balanced, Regexp::Common, Text::PromptBalanced, Parse::RecDescent, or perl-lisp.
Lastly, I would recommend thinking of a better title for your post. CPCFG_count.pl may work for you as the script's filename, but PerlMonks titles should convey some information. It helps to think of the title as the place to put key words. Thanks.
A word spoken in Mind will reach its own level, in the objective world, by its own weight