InsertSQL

Hi guys, one final code snippet on this particular script anyway.
Id like to work out what the following does.

========================================================================
      open(SQL,"import/${Store}_site.sql");
      while (<SQL>) {
         $SQL = $_;
         if ($SQL =~ /^\$/) {
            #
            #
            my $NewSystem = "";
            my $InsertSQL = "";
            eval $SQL;
            if ($NewSystem) {
               my $SQL = "SELECT System FROM Systems where SystemName = '$NewSystem'";
               my $sth = $dbh->prepare($SQL);
               $sth->execute() || Abort();
               my ($OnFile) = $sth->fetchrow_array();
               $sth->finish();
               if (!$OnFile) {
                  Log "      HealthCheck $InsertSQL";
                  $dbh->do($InsertSQL) || Abort();
               }
            }
         }
         else {
            $dbh->do($SQL) || Abort();
         }
      }
      close(SQL);

I know that in the above code, it adds/inserts a new record for a newly found system, but I cant see how, because i cant see the insert statement. Would this code be in the .sql file?
I dont know why also...

           my $NewSystem = "";
            my $InsertSQL = "";

are set to ""
and why the my keyword is used here.

Any help deciphering this would be appreciated.
LVL 1
Simon336697Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
FishMongerConnect With a Mentor Commented:
Which part(s) do you not understand?

open(SQL,"import/${Store}_site.sql"); # open file in read mode, without verifing that it succeeded
while (<SQL>) { # loop through the file line by line
$SQL = $_;
if ($SQL =~ /^\$/) { # is there a $ at the begining of the line
   #
   #
   my $NewSystem = ""; # create a lexical var and initialize it to an empty stringassign var a
   my $InsertSQL = ""; # same as above
   eval $SQL; # execute the contents of $SQL without throwing an exception - read perldoc -f eval

   if ($NewSystem) { # boolean test to see if var equates to true, which in this case it never will
      # since $NewSystem equates to false, the following lines will never execute
      my $SQL = "SELECT System FROM Systems where SystemName = '$NewSystem'"; # select statement where SystemName = ''
      my $sth = $dbh->prepare($SQL);
      $sth->execute() || Abort(); # execute the prepared statement or run Abort() sub if it fails
      my ($OnFile) = $sth->fetchrow_array(); # retrieve the first matching row, placing it in an array referance
      $sth->finish(); # "tell" the dbh that we're done fetching
      if (!$OnFile) { # if var evaluates to false, i.e., we didn't fetch anything
         Log "      HealthCheck $InsertSQL"; # not sure if LOG is meant to be a filehandle or  subroutine
         $dbh->do($InsertSQL) || Abort(); # since $InsertSQL is 'empty', the $dbh->do statement will fail and the Abort() sub will be executed
      }
   }
}
else {
   $dbh->do($SQL) || Abort(); # prepare and execute $SQL statement and run the Abort() sub, if it fails
# since you never used chomp to remove the \n line ending from $SQL, I suspect that the $dbh->do statement would fail
}
}
close(SQL); # close the filehandle

If we remove the parts that are unneeded and won't be executed, this is what that code boils down to:
open(SQL,"import/${Store}_site.sql");
 
while (my $SQL = <SQL>) {
 
   if ($SQL =~ /^\$/) {
      # do nothing
   }
   else {
      $dbh->do($SQL) || Abort();
   }
}
close(SQL);

Open in new window

0
 
Simon336697Author Commented:
Hi Fish!

Your explanation was just amazing thank you.

Really appreciate it and it makes perfect sense what you have said.

Thanks again Fish :>)
0
All Courses

From novice to tech pro — start learning today.