Different output when selecting from Postgresql database table via Perl script

I am facing a rather strange situation. Through a perl script, I connect to the Postgresql database, and I select specific columns from a specific table. The result of the SELECT query from the database is saved in a file. When the script is run a second time, the contents of the file from the first selection are transferred to another file, and the actual file is populated with a new select output. Then I compare these two files. They must be the same at run time twice in the script. The problem is that when I run the script twice to compare, they differ on the same line, when I execute the script a third time, the output will be the same as it was in frist time, and when I execute the fourth time, the output is the same as the second time, and these outputs are repeatedly alternated. I tried 4 different methods how to extract selected rows from a table to a file,but still the same situation, however, the contents of the table in the database remains the same.

See the code below:

use strict;
use warnings;
use DBI;
use DBD::Pg;
use File::Copy;
use File::Compare;
use List::Compare;
use MIME::Lite;
use Data::Dumper;
use Sys::Hostname;

my $qry1 = "select id, name from nms_incident_config where enable = 'f' order by name asc";
my $ltraps = '/tmp/traps.list' ;
my $ntraps = '/opt/tools/trapcfg_check/actual_traps.list';

if(-e $ntraps ) {
    print "Actual trap list exists \n";
    move("$ntraps","$ltraps") or die "Copy failed! :$!";
      };

open my $out ,">", "/opt/tools/trapcfg_check/actual_traps.list";

#connect to db and run selects for further use

my $db = DBI->connect( "dbi:Pg:dbname=<db_name>;host=localhost;","postgres", "*******", {'RaiseError' => 1});

my $qry1s = $db-> prepare("$qry1");
$qry1s->execute();

METHOD 1

  while (my @row = $qry1s -> fetchrow_array()) {
     $" = ",";
     print $out "@row \n";
  }

METHOD 2

while (my @row = $qry1s->fetchrow_array()) {
  #print $out "$row[0],$row[1]\n";
  print $out join(", ", @row), "\n"
}

METHOD 3

my ($id,$name);
$qry1s->bind_columns(\($id, $name));
while ($qry1s->fetch){
  print $out "$id, $name\n"
}

METHOD 4

my $row;
$row = $qry1s->fetchall_arrayref([]);
foreach my $r (@{$row})
{
   print $out join(", ", @{$r}), "\n";
}

In scripts, only one method is uncommented and used, the remaining comments are commented.

$db->disconnect();

open F ,"</opt/tools/trapcfg_check/actual_traps.list" or die $!;
open F1 , "/tmp/traps.list" or die $!;

my @ntraps=<F>;
my @ltraps=<F1>;

my @lonly ;
my @nonly ;

   if ( compare( $ltraps,$ntraps ) == 0 ) {
      print "Files are same \n" ;
      die "Files are same\n";
      };

   if ( compare( $ltraps,$ntraps ) ne 0 ) { print "Files are different\n";

      my $tc = List::Compare->new(\@ltraps, \@ntraps );
         @lonly = $tc->get_complement;
#        @nonly = $tc->get_unique;
      print "Different row/s in new trap list: "."@lonly\n";

   };

Outputs>

# ./tcheck_2.pl
Actual trap list exists

Files are same
Files are same

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# ./tcheck_2.pl
Actual trap list exists

Files are different
Different row/s in new trap list: 2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP

# ./tcheck_2.pl
Actual trap list exists

Files are same
Files are same

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# ./tcheck_2.pl
Actual trap list exists

Files are different
Different row/s in new trap list: 2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP

Can you help me? :) Thanks

Best wishes Yaro

+4
source share
1 answer

As Borodin suggested in the comments , I needed to add

close $out

after the cycle print.

-1
source

Source: https://habr.com/ru/post/1671345/


All Articles