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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.