Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: bug or curly bracket hell?

by GrandFather (Saint)
on Jan 13, 2023 at 23:02 UTC ( [id://11149581]=note: print w/replies, xml ) Need Help??


in reply to Re^2: bug or curly bracket hell?
in thread bug or curly bracket hell?

First off: 'code smells' are coding styles that are fragile or hard to maintain (probably because they are hard to read). use strict helps address some forms of code smell if used sensibly. One thing that can make a huge difference is to ensure variables are used in the smallest sensible scope - strict helps that by telling you where an undeclared variable is first used.

A very closely related technique is to initialise scalar variables (variables that start with $) where they are declared. Sometimes array and hash variables can be usefully initialised where they are declared too, but often they are used to accumulate entries in following code and don't need an explicit initialisation value at declaration (they are born empty).

The golden rule: Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

Strictures, especially strict, tell you about things that are easy to get wrong and hard to find, like changing the spelling of an identifier. Using lexical variables (variables declared with my) limits their scope to the enclosing block (within the enclosing { } pair) which helps in understanding how variables are used and avoids misusing variables.

Fixing a bad smell can often end up with a jolly good clean out of the whole fridge to everyone's benefit. Adopting the use of strictures is a good start to cleaning up smelly code.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^4: bug or curly bracket hell?
by Bod (Parson) on Jan 14, 2023 at 01:11 UTC
    Adopting the use of strictures is a good start to cleaning up smelly code

    Absolutely seconded!

    I resisted at first but since following the teaching wise monks, my code has been less smelly, giving me an easier and more pleasant coding life.

Re^4: bug or curly bracket hell?
by MoodyDreams999 (Beadle) on Jan 24, 2023 at 23:03 UTC
    I was able to shorten my code by 100 lines taking in the advice. I can use it with perl -c, but I've run into another issue.. I'm getting use of uniniatlized value for each DB varible in the SQL part of the code to connect to the db, for example use of uniniatlized value $VARDB_database in concatenation or string at line 29. Each line the program reads it says, argument "SRC 125,Emma,,boston,,in,48011,8123456787" isnt numeric in array element at line 118. input same line is: SRC 125 "Emma Boston IN 48011 8127429212 emmaboston2011@icloud.com." its a 20000 line excel doc. Tried googling the error codes and ask my coworker but no such luck, my coworker and the one before him have never bothered with strict so all of our perl scripts don't use it. This is what I have now.
    print " use strict; use warnings; use Excel::Writer::XLSX; use DBI; use Time::Piece; use Math::Round; #path to astguiclient configuration file: my $PATHconf = '/etc/astguiclient.conf'; my ( $PATHlogs, $VARserver_ip, $VARDB_server, $VARDB_database, $VARDB_user, $VARDB_pass, $VARDB_custom_user, $VARDB_custom_pass, $VARDB_port); open('conf', "$PATHconf") || die "can't open $PATHconf: $!\n"; my @conf = <conf>; close('conf'); my $i=0; foreach(@conf){ my $line = $conf[$i]; my $t= "".$i; my $dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$ +VARDB_port", "$VARDB_user", "$VARDB_pass") or die "Couldn't connect to database: " . DBI->errstr; my $rowCount = 0; my $filename = "Output012023.xlsx"; my $workbook = Excel::Writer::XLSX->new( $filename ); open(FH, "<", "Debt.csv" ) or die; my $worksheet = $workbook->add_worksheet('List'); my $worksheet2 = $workbook->add_worksheet('List2'); my $format = $workbook->add_format(); my $format1 = $workbook->add_format(); #my $format2 = $workbook->add_format(); $format->set_center_across(); $format1->set_color( 'red' ); #$format2->set_bold(); #print join("\n", map { s|/|::|g; s|\.pm$||; $_ } keys %INC); #proper format must be used for the file or it will give an error $worksheet->set_column( 8, 8, 20 ); $worksheet->set_column( 10, 10, 15 ); $worksheet->set_column( 4, 4, 18 ); #$worksheet->set_column( 5, 5, 15 ); #$worksheet->set_column( 12, 12, 65 ); $worksheet->write(0, 0, "source_id" ); $worksheet->write(0, 1, "first_name" ); $worksheet->write(0, 2, "middle" ); $worksheet->write(0, 3, "last_name" ); $worksheet->write(0, 4, "address1" ); $worksheet->write(0, 5, "city"); $worksheet->write(0, 6, "state"); $worksheet->write(0, 7, "postal_code"); $worksheet->write(0, 8, "phone_number"); $worksheet->write(0, 9, "address3"); $worksheet->write(0, 10,"province"); $worksheet->write(0, 11, "email"); my $rowCount1 = my $rowCount2 = 0; #$worksheet2->set_column( 8, 8, 20 ); #$worksheet2->set_column( 10, 10, 15 ); #$worksheet2->set_column( 4, 4, 20 ); #$worksheet2->set_column( 5, 5, 15 ); #$worksheet2->set_column( 12, 12, 62 ); $worksheet2->write(0, 0, "source_id" ); $worksheet2->write(0, 1, "first_name" ); $worksheet2->write(0, 2, "middle" ); $worksheet2->write(0, 3, "last_name" ); $worksheet2->write(0, 4, "address1" ); $worksheet2->write(0, 5, "city"); $worksheet2->write(0, 6, "state"); $worksheet2->write(0, 7, "postal_code"); $worksheet2->write(0, 8, "phone_number"); $worksheet2->write(0, 9, "address3"); $worksheet2->write(0, 10,"province"); $worksheet2->write(0, 11, "email"); my $test=0; my $oZ=0; #RowCount1 writes for the whole page then 100 lines later it goes +to rowcoutn2 may need changed while (<FH>){ chomp; my @t= split(',',$_); if (length($t[8]) == 10) { # && (length($t[7])==4 || length($ +t[7])==5)) { if (!defined($t[7]) || $t[7] eq ''){ my $stmt ="SELECT postal_code, state FROM asterisk.vic +idial_postal_codes WHERE state='".$t[6]."' limit 1;"; my $sth = $dbhA->prepare($stmt) or die "preparing: ",$ +dbhA->errstr; $sth->execute or die "executing: $stmt ", $dbhA->errst +r; my $sthrows= $sth->rows; my $Count=0; #$z=""; while($sthrows>$Count) { my @ary = $sth->fetchrow_array; $worksheet->write($rowCount1+1, $_, $t[$_]); $worksheet->write($rowCount1+1, $_, $ary[$_]); $Count++; } } else { if (!defined($t[6]) || $t[6] eq '' || length($t[6])>2 +|| length($t[6])<2){ my $stmt = "SELECT state, postal_code FROM asteris +k.vicidial_postal_codes WHERE postal_code='".$t[7]."' Limit 1;"; my $sth = $dbhA->prepare($stmt) or die "preparing: + ",$dbhA->errstr; $sth->execute or die "executing: $stmt ", $dbhA->e +rrstr; my $sthrows=$sth->rows; my $Count=0; while($sthrows>$Count){ my @ary = $sth->fetchrow_array; $worksheet2->write($rowCount2+1, $_, $t[$_]); $worksheet2->write($rowCount2+1, $_, $ary[$_]) +; $Count++; } } else { $worksheet->write($rowCount1+1, $_, $t[$_]); } $rowCount1++; } } else { my $z=""; if (!defined($t[7]) || $t[7] eq '' || length($t[7])>5 || l +ength($t[7])<=3){ my $stmt="SELECT postal_code, state FROM asterisk.vici +dial_postal_codes WHERE state='".$t[6]."' and postal_code not in ('00 +501','00544') limit 1;"; my $sth = $dbhA->prepare($stmt) or die "preparing: ",$ +dbhA->errstr; $sth->execute or die "executing:$stmt ", $dbhA->errstr +; my $sthrows= $sth->rows; my $Count=0; $oZ=$t[7]; while($sthrows>$Count){ my @ary = $sth->fetchrow_array; $z=$ary[0]; $worksheet2->write($rowCount2+1, $_, $t[$_]); $worksheet2->write($rowCount2+1, $_, $ary[$_]); $Count++; } } else { if (!defined($t[6]) || $t[6] eq '' || length($t[6])>2 +|| length($t[6])<2){ my $stmt="SELECT state, postal_code FROM asterisk. +vicidial_postal_codes WHERE postal_code='".$t[7]."' Limit 1;"; my $sth = $dbhA->prepare($stmt) or die "preparing: + ",$dbhA->errstr; $sth->execute or die "executing: $stmt ", $dbhA->e +rrstr; my $sthrows=$sth->rows; my $Count=0; while($sthrows>$Count){ my @ary = $sth->fetchrow_array; $worksheet2->write($rowCount2+1, $_, $t[$_]); $worksheet2->write($rowCount2+1, $_, $ary[$_]) +; $Count++; } } else { $worksheet2->write($rowCount2+1, $_, $t[$_]) } } if (length($t[8]) < 10 || length($t[8]) > 10){ $worksheet2->write($rowCount2+1, 12, 'phone is incorre +ct.', $format1); } if (defined($z) || $z ne ''){ $worksheet2->write($rowCount2+1, 12, $oZ.' Zip code ha +s been updated '.$z.'.', $format1); } elsif (length($t[8]) < 10 || length($t[8]) > 10) { $worksheet2->write($rowCount2+1, 12, ' Zip code update +d, but has bad phone. ', $format1); } $rowCount2++; } } $rowCount++; } my $workbook->close(); #print $rowCount1.'-'.$rowCount2.'-'.$rowCount; close(FH);\n";

      You have a bogus 'print "' at the start of your code. The closing quote for that string is 'open('conf', "' (many lines later). All of the apparent use statements and my declarations, in the intervening code, are just part of the string; they are not Perl statements that will be executed.

      — Ken

        Note though the last line:

        close(FH);\n";

        :-D

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        my $PATHconf = '/etc/astguiclient.conf'; # declared and initialized
      
        my ( # declared with "my" but not initialized
        $PATHlogs,
        $VARserver_ip,
        $VARDB_server,
        $VARDB_database,
        $VARDB_user,
        $VARDB_pass,
        $VARDB_custom_user,
        $VARDB_custom_pass,
        $VARDB_port); 
      
      You need to initialize your declared variables by defining their values:
        my $PATHconf       = '/etc/astguiclient.conf';
        my $PATHlogs       = '/path/to/logs';
        my $VARserver_ip   = '1.2.3.4';
        my $VARDB_server   = 'your_db_server';
        my $VARDB_database = 'name_of_database';
        my $VARDB_user     = 'the_db_username';
        my $VARDB_pass     = 'the_db_password';
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-19 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found