Hi everybody,
I've been writing perl for a while now, but I still find I'm writing "Baby Talk" perl - perl that's so heavily indented and makes such overuse of 'if' statements that it's just ugly
I want to improve my coding style to make it more 'perlish', yet still maintain readability etc.
Here's a example of some more offensive code.
It's a section of a CGI script (which does use warnings and strict) to do stock control of some items in a MySQL database.
I'm basically asking for style advice, or comments, because at the moment it just feels like I'm underusing the language.
Any comments gratefully accepted.
my $action = param("ACTION"); if($action) { #Bond the built-in and accessory components to the parent. if($action eq "BOND") { my $main = param("MAIN"); my @built = param("BUILT"); my @acc = param("ACC"); my $query = "SELECT location FROM demstock2 WHERE ds_i +d=$main"; my $sth = $db->prepare($query); $sth->execute or print "Can't execute <pre>$query</pre +>: " . $db->errstr . "<br>\n"; my $mainlocation = $sth->fetchrow_hashref; if($mainlocation->{"location"} ne "STOCK") { print b("Refusing to bond items to $main, as i +t isn't in main stock") . br(); } else { $sth->finish(); for my $acc (@acc) { my $query = "SELECT curr_parent_id FRO +M demstock2 WHERE ds_id=$acc"; my $sth = $db->prepare($query); $sth->execute() or print "Can't execut +e $query: " . $sth->errstr . "<br>\n"; my $current_status = $sth->fetchrow_ha +shref; $sth->finish(); if($current_status->{"curr_parent_id"} + != 0) { print "Refusing to modify the +status of demstock $acc. It has already been moved<br>\n"; } else { my $query = "UPDATE demstock2 +SET curr_parent_id=$main WHERE ds_id=$acc"; my $sth = $db->prepare($quer +y); $sth->execute or print "Can't +execute <pre>$query</pre>: " . $db->errstr . "<br>\n"; $sth->finish(); $query = "INSERT INTO action " +; $query .= "(action_id, actio +n_type, ds_id, occurred, ref_no, staff_id) VALUES" ; $query .= "(NULL, 'MOVE +', $acc, CURRENT_TIMESTAMP, \"0,$main\", $caluser)"; $sth = $db->prepare($query); $sth->execute or print "Can't +execute <pre>$query</pre>: " . $db->errstr . "<br>\n"; $sth->finish; } } for my $built (@built) { my $query = "SELECT curr_parent_id FRO +M demstock2 WHERE ds_id=$built"; my $sth = $db->prepare($query); $sth->execute() or print "Can't execut +e $query: " . $sth->errstr . "<br>\n"; my $current_status = $sth->fetchrow_ha +shref; $sth->finish(); if($current_status->{"curr_parent_id"} + != 0) { print "Refusing to modify the +status of demstock $built. It has already been moved<br>\n"; } else { my $query = "UPDATE demstock2 +SET curr_parent_id=$main WHERE ds_id=$built"; my $sth = $db->prepare($quer +y); $sth->execute() or print "Can' +t execute $query: " . $sth->errstr . "<br>\n"; $sth->finish(); $query = "INSERT INTO action " +; $query .= "(action_id, actio +n_type, ds_id, occurred, ref_no, staff_id) VALUES" ; $query .= "(NULL, 'MOVE +', $built, CURRENT_TIMESTAMP, \"0,$main\", $caluser)"; $sth = $db->prepare($query); $sth->execute or print "Can't +execute <pre>$query</pre>: " . $sth->errstr . "<br>\n"; $sth->finish; } } } } }
The five trailing braces just feel "wrong" to me, in particular
cheers.
davis

In reply to A question of style. by davis

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, details, 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, summary, 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.