为什么这个Perl代码在不同的机器上运行时会返回不同的数据类型?

Seb*_*bas 1 postgresql perl mod-perl

我是维护合同的开始,我以前从未做过Perl,这就是我发现的.

我有:

sub record {
    my ( $dbh, $sth, $sql, %rs, %arg, @alias, $key, %default );
    %default = ( db => '*', tabla => '*', campos => '*', condicion => '', campos => '*', alias => '*', visible => 'si' );
    %arg = @_;

    if ( $arg{campos} ) { $default{alias} = $arg{campos}; }
    foreach $key ( keys %default ) {
        if ( !exists $arg{$key} ) { $arg{$key} = $default{$key}; }
        if ( exists $arg{$key} && $arg{$key} eq '' ) { $arg{$key} = $default{$key}; }
        if ( $arg{$key} eq '*' ) { &msj( "Error !!!", "$key is needed" ); return; }
    }

    @alias = split /,/, $arg{alias};

    $dbh = DBI->connect( "dbi:Pg:dbname=$arg{db}; host=$ipserver; port=5432", "postgres", "xxxx" ) or die "Error: $DBI::errstr";

    if ( !$DBI::errstr ) {
        $sql = "SELECT $arg{campos} FROM $arg{tabla} $arg{condicion}";
        if ( $arg{visible} eq 'si' ) { &msj( "Consulta a la base de datos $arg{db}", $sql ); }
        $sth = $dbh->prepare($sql) or die "No se ha preparado: $DBI::errstr";
        $sth->execute;
        @rs{@alias} = ();
        if ( $DBI::rows > 0 ) {
            $sth->bind_columns( map { \$rs{$_} } @alias );
        }
        return ( \%rs, sub { $sth->fetch() } );
        $sth->finish;
        $dbh->disconnect;
    } else {
        &mensaje( "Error !!!!", "No access to $arg{db}" );
        exit;
    }
   }
Run Code Online (Sandbox Code Playgroud)

要使用它,我有类似的东西

( $rs, $fetch ) = record( db => "infodfsisadmon", tabla => "login", condicion => "where usuario='$FORM{usuario}' and clave='$FORM{clave}'", campos => "acceso,referencia,id_modulo,uaa,nivel_acceso,privilegios,activo,correo", visible => "si" );
# Show me the record
print "rs ->" . Dumper $rs;
Run Code Online (Sandbox Code Playgroud)

当我在Centos 5.6的服务器上运行时,我得到:

rs ->$VAR1 = { 
    'nivel_acceso' => '{"",NL,NL,NL,NL,"","","","","","","","","","","","","","","","","","","","","","","","",""}', 
    'correo' => 'xxxx@gmail.com', 
    'privilegios' => '{ADM,ADMINISTRADOR,ADM,ADM,1:AMEI:2:AMEI:3:AMEI:4:AMEI:5:AMEI,"","","","","","","","","","","","","","","","","","","","","",""," ","",""}', 
    'acceso' => '{t,t,t,t,t,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f}', 
    'referencia' => '{/cgi-bin/infodf/nomina/index.cgi,/cgi-bin/infodf/contable/index0.cgi,/cgi-bin/infodf/presupuesto/index0.cgi,/cgi-bin/infodf/nomina_fonacot/index.cgi,/cgi-bin/infodf/recmat/index.cgi,"","","","","","","","","","","","","","","","","","","","","","","","",""}', 
    'id_modulo' => '{1,2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}', 
    'uaa' => '{"",002,002,002,002,"","","","","","","","","","","","","","","","","","","","","","","","",""}', 
    'activo' => '{t,t,t,t,t,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f}' 
    };
Run Code Online (Sandbox Code Playgroud)

当我在Centos 5.7上运行时,我得到的是:

rs ->$VAR1 = { 
    'nivel_acceso' => [ 'NL', 'NL', 'NL', 'NL', 'NL', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ], 
    'correo' => 'rigoaj@hotmail.com', 
    'privilegios' => [ 'ADM', 'ADMINISTRADOR', 'ADM', 'ADM', '1:AMEI:2:AMEI:3:AMEI:4:AMEI:5:AMEI', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ' ', '', '' ], 
    'acceso' => [ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
    'referencia' => [ '/cgi-bin/infodf/nomina/index.cgi', '/cgi-bin/infodf/contable/index0.cgi', '/cgi-bin/infodf/presupuesto/index0.cgi', '/cgi-bin/infodf/nomina_fonacot/index.cgi', '/cgi-bin/infodf/recmat/index.cgi', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ], 
    'id_modulo' => [ 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
    'uaa' => [ 'CONS', '002', '002', '002', '002', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ], 
    'activo' => [ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] 
};
Run Code Online (Sandbox Code Playgroud)

Centos 5.6上的perl5(版本5版本8颠覆8)是perl5(版本5版本8颠覆8),但5.7中的是64位而5.6中的是32.

两台服务器都运行postgresql-8.1,安装的rpms是:

5.6
postgresql-8.1.22-1.el5_5.1
postgresql-contrib-8.1.22-1.el5_5.1
postgresql-docs-8.1.22-1.el5_5.1
postgresql-jdbc-8.1.407-1jpp.4
postgresql -libs-8.1.22-1.el5_5.1
postgresql-odbc-08.01.0200-3.1
postgresql-pl-8.1.22-1.el5_5.1
postgresql-python-8.1.22-1.el5_5.1
postgresql-server -8.1.22-1.el5_5.1
postgresql-test-8.1.22-1.el5_5.1

5.7
postgresql-8.1.23-1.el5_7.3
postgresql-devel-8.1.23-1.el5_7.3
postgresql-libs-8.1.23-1.el5_7.3
postgresql-server-8.1.23-1.el5_7. 3

截至DBI和DBD:Pg:
5.6
DBI 1.52
DBD :: Pg 1.49

5.7
DBI 1.52
DBD :: Pg 2.18.1

我不知道为什么会有这种差异.您的提示,技巧和解释都是受欢迎的.

Jon*_*ler 7

我谦虚地确信您在软件堆栈中的两台机器之间存在版本差异,其中包括:

  • Perl的
  • DBI
  • DBD :: Pg(PostgreSQL)
  • PostgreSQL客户端库(ODBC,或其他)
  • PostgreSQL DBMS

这些层中的至少一个并且可能几个是不同的.您声明服务器是PostgreSQL 8.1; 这表明问题出在客户端库或DBD :: Pg中.它不太可能是Perl版本或DBI版本的问题,但这可能取决于两个版本(Perl和DBI)的差异.这种差异不太可能是由o/s版本引起的.

较旧的代码(在Centos 5.6上)返回数组中数据的字符串形式.较新的代码更自然地处理数组.这仍然没有解释$rs->uaa->[0]旧的和新的第一个(空)字段之间的区别,但确实占其余的大部分.


Perl DBI对可能相关的绝对版本的数量异常严格.但是,我认为您的问题源于旧DBD::Pg模块.

根据http://search.cpan.org/,

  • DBD :: Pg:CPAN的最新版本是2.99.9_1从2011年6月开始; 2.18是从2011年5月起; 1.49不在CPAN上,但是1.32是2004年2月,而2.5.0是2008年3月).
  • DBI:CPAN的最新版本是2010年12月的1.616; 1.52不是CPAN,而是1.53是2006年11月.

我建议在两台计算机上升级到相同的更新版本,或者将旧版本与旧版本的新版本保持一致.DBI版本不太可能成为问题的根源,但升级到最新版本仍然是好的.