pepziman pepziman - 5 months ago 19
Perl Question

migrating from perl to php

I have no experience in Perl at all, and also beginner in PHP. I need to migrate the old company system using Perl to new one using PHP. Here is some code in Perl I didn't understand:

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};
}


Can someone explain what does the code mean and what it will become in PHP? Also, what does
$a->{$b}
mean in Perl?

I've been trying to figure it out since 3 days but still having difficulties dealing with it.

Answer

I don't use php, but I try to explain the code in general:

# 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};
}

You can read more about Perl hashes, for example here (I repeat myself): http://perlmaven.com/perl-hashes