从perl迁移到php

时间:2016-06-21 07:00:41

标签: php windows perl laravel

我根本没有Perl的经验,也是PHP的初学者。我需要使用PHP将使用Perl的旧公司系统迁移到新系统。以下是Perl中的一些代码我不明白:

while (my $data=$query->fetchrow_hashref) {
        push @oid_list, $data->{oid};
        push @{$snmp_order->{$sensor}}, $data->{function};
        $oid_hash->{$sensor}->{$data->{function}}->{oid}=$data->{oid};
        $oid_hash->{$sensor}->{$data->{function}}->{scale}=$data->{scale};
    }

有人可以解释代码的含义以及它在PHP中的含义吗?另外,$a->{$b}在Perl中意味着什么?

自从3天以来我一直试图解决这个问题,但仍然难以处理它。

2 个答案:

答案 0 :(得分:3)

我不使用php,但我试着解释一般的代码:

# while there is data in the object that is returned by calling the method "fetchrow_hashref" of the object $query: it means, get a single data row from the database 
while (my $data=$query->fetchrow_hashref) {

    # add the value stored in field "oid" from the hash "data" to array "oid_list"
    # what is hash? kind of array indexed by strings. Red more about hashes here: http://perlmaven.com/perl-hashes
    push @oid_list, $data->{oid};

    # add the value stored in field "function" of the hash "data" to 
    # an array that is found in a reference, which reference is stored in hash "snmp_order" under 
    # a field that is named the same as the value of variable $sensor
    push @{$snmp_order->{$sensor}}, $data->{function};

    # now: the data->oid is added to oid_hash. I will use array notation to explain where it lands:
    # oid_hash[$sensor][$data->{function}][oid] =  $data->{oid}
    $oid_hash->{$sensor}->{$data->{function}}->{oid}=$data->{oid};

    # oid_hash[$sensor][$data->{function}][scale] =  $data->{scale}
    # $sensor and $data->{function} are variables!
    $oid_hash->{$sensor}->{$data->{function}}->{scale}=$data->{scale};
}

您可以阅读有关Perl哈希的更多信息,例如此处(我重复一遍):http://perlmaven.com/perl-hashes

答案 1 :(得分:3)

好吧,因为我不是一个php程序员,我只能告诉你perl-code的含义。也许它会对你有所帮助!

#an query to the Database was fired through the DBI-module from Perl
#the result is temporarily stored in the $query but has to be fetched
#before further use. Since the result could contain multiple rows the $data
#is filled with each row while the ->fetchrow_hashref() function returns a hash of
#data representing one returned row of the query. The keys of the hash are the column
#names of the database-table
while(my $data = $query->fetchrow_hashref){...}

@表示Perl中的数组。所以@oid_list被视为一个。因此,可以将值$data->{oid}推入其中(push @oid_list, $data->{oid})。

语法$data->{oid}有点复杂。想象一下,$data是一个引用(只是一个指针)到Hash(或其他语言,如java,称为Map)。但您更愿意访问哈希此引用。因此,您使用-> 取消引用指针并访问$data指向的实际哈希值。在$data->{oid}后,您可以访问Hashreferenceby oid中密钥$data后面的值。

push @{$snmp_order->{$sensor}}, $data->{function};中发生类似的事情。但在这里,您首先必须访问引用$snmp_order->{$sensor}后面的真实数组。这里,引用哈希的一个键包含一个数组引用,它将值$data->{function};输入其中。

代码的其余部分是这两件事。程序员只是将获取的数据库行$data中的不同值分配给大型哈希引用$oid_hash的不同键。在{}中,您将始终找到所访问密钥的名称。