Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: bug or curly bracket hell?

by MoodyDreams999 (Beadle)
on Jan 13, 2023 at 19:29 UTC ( [id://11149577]=note: print w/replies, xml ) Need Help??


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

okay lets say I have this code to access a server/database and I have repeating variables and these are the repeating errors I have on strict. Variable "$VARDB_DATABASE" is not imported AT PROJECT.PL LINE 43. I get this for the other variables as well then Global symbol "$VARDB_DATABASE" requires explicit package name AT PROJECT LINE 43. Keep in mind this works with out strict for me. (paragraph)

#path to astguiclient configuration file: $PATHconf = '/etc/astguiclient.conf'; open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n"; @conf = <conf>; close(conf); my $i=0; foreach(@conf){ $line = $conf[$i]; $line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi; if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) ) {$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;} if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) ) {$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;} if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) ) {$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;} if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) ) {$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;} if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) ) {$VARDB_database = $line; my $VARDB_database =~ s/.*=//gi;} if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) ) {$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;} if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) ) {$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;} if ( ($line =~ /^VARDB_custom_user/) && ($CLIDB_custom_user < 1) ) {$VARDB_custom_user = $line; $VARDB_custom_user =~ s/.*=//gi +;} if ( ($line =~ /^VARDB_custom_pass/) && ($CLIDB_custom_pass < 1) ) {$VARDB_custom_pass = $line; $VARDB_custom_pass =~ s/.*=//gi; +} if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) ) {$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;} $i++; } if (!$VARDB_port) {$VARDB_port='3306';} use strict; use warnings; use Excel::Writer::XLSX; use DBI; use Time::Piece; use Math::Round; 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;

Replies are listed 'Best First'.
Re^5: bug or curly bracket hell?
by Anonymous Monk on Jan 13, 2023 at 20:36 UTC
    if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) ) {$VARDB_database = $line; my $VARDB_database =~ s/.*=//gi;}
    With strict enabled you can't do certain things that make no sense, like declaring a variable with "my" after you already used that variable.
      Well I guess that means I will have to rewrite it all if I wanna use strict, thank you for the guidance
        To fix this line, just remove the "my" before "$VARDB_database":
        if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) ) {$VARDB_database = $line; my $VARDB_database =~ s/.*=//gi;}
        Using strict and warnings will save you from bugs like the above which fail silently:

        use strict; use warnings; #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); # Now perl knows the variables you will use, so if there is # a typo later like "$PATHlog" instead of "$PATHlogs" then # perl will tell you how to fix it, instead of just failing # silently (like crappy PHP and Python :^) open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n"; my @conf = <conf>; close(conf); my $i=0; foreach(@conf){ my $line = $conf[$i];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-29 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found