Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Yeah, I have a few comments, presented here in decreasing order of importance.

First and foremost, you should be useing strict and warnings.
When you add those, you will get a whole slew of errors, which tell you the next thing you should do:

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

while ( <COLL> ) { chomp; @coll = split /:/; $basicColl{$coll[0]} = $coll[1]; }
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,

open(CORP,"<$path") or die ("Error...: $!");
could be written as
open CORP,"<$path" or die "Error...: $!";

You might consider using the "ternary operator". It would significantly clean up certain situations, such as this one:

if($mother) { $lhs="$mothers[$depth]"."_$mothers[$depth-1]"; } else { $lhs="$mothers[$depth]"."_$aunties[$depth]"; }
That could be written as
$lhs = join '_', $mothers[$depth], $mother ? $mothers[$depth-1] : $aunties[$depth];

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

In reply to Re: CPCFG_count.pl by jdporter
in thread Contextualized weighted grammar generator by raptur

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (3)
    As of 2020-11-27 03:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?