Does Perl change value in conditional expression before entering conditional?

I am working on a Perl script to help automate the scanning of machines on our network. I am not a programmer by profession, but, nevertheless, this project was assigned to me, and I'm completely dumb. Before I explain the nature of what is haunting me, let me explain what I am doing.

Basically, this script will run every n hours. At startup, it will check the file containing the log of active IP addresses and check them in the DHCP log to highlight only those that are static. They are then placed into a hash (new if the initialization flag is loaded using Storable otherwise), the key being the IP address inside the MAC [0] array and the β€œlast verified” date [1] originally set to 19700101 The next part of the script compares the date between today's date and the "last checked" date - and if it is at a certain threshold, it sends a request to our scanner.

The problem that has lost me is that when the date is checked, it seems to me that before entering the conditional value, the date value is set (updated "last scanned"). Although this does not seem likely to me, this is the only opportunity I can think of. Here are the relevant code snippets:

Code that adds IP / MAC to the hash

 if(init == 1){
            %SCAN = ();

            @data = ();

            foreach $key (keys %IPS){

                    $unsavedDB = 1;

                    $data[0] = $IPS{$key};
                    $data[1] = 19700101;

                    print $data[1];

                    $SCAN{$key} = \@data;
            }
 }else{
            #repeat of the above code, but with a if(exists...) to prevent duplicates from being added to the hash that is loaded via storables.
 }

A code that checks the date (which was set earlier and will be on 20120726). There are only comments between the code above and below

    $scanned = 0;

    foreach $key (keys %SCAN){

            $lastScanned = $SCAN{$key}[1];

            if(($date - $lastScanned) > $threshold){
                    $unsavedDB = 1;

                    $toScan = ${$key}[0];

                    #omitted data for security reasons, just basically forms a string to send to a scanner

                    $SCAN{$key}[1] = $date;

                    $scanned++;
            }
    }

    print "finished. $scanned hosts queued\n";

, , , - print $lastScanned "if (($ date...) {" , , $date , $SCAN {$ key} [1] = $date; ', "19700101", , . ? $SCAN {$ key} [1] , .

, . -, .

!

+5
2

@data , ,

$SCAN{$key} = \@data;

$SCAN{$key} @data. , %SCAN , , -, , .

. , @data $SCAN{$key},

$SCAN{$key} = [ @data ];

, , my β€” :

foreach $key (keys %IPS) {
        $unsavedDB = 1;

        my @data;  # <--- this line is new!

        $data[0] = $IPS{$key};
        $data[1] = 19700101;

        print $data[1];

        $SCAN{$key} = \@data;
}

, , , , Perl , .

, , , strict . Perl, , , #!:

use strict;
use warnings;

strict , , warnings , , ( , ).

, , script my ( our), strict . , , , , . ( , .) , ,

foreach my $key (keys %IPS) {

while (my $line = <>) {

Ps. , :

# repeat of the above code, but with ...

, , , , - -β€” " ."

, , , -, , , . , . , , , ,

if (not $init and exists ...) {

.

+8

, , %SCAN , @data , $SCAN{<anything>}[1] IP-.

, @data

$SCAN{$key} = [ $IPS{$key}, '19700101' ];

, , -.

, , , $date - $lastScanned: . 31-JAN-2012 1-FEB-2012 20120201 - 20120131 70!

, , , Time::Piece, (.. Perl Perl 5.9) ​​.

, use strict use warnings,

use Time::Piece;

, ,

my $initial = localtime(0);

my $date = localtime;

, ,

print $initial, "\n";
print $date, "\n";

-

Thu Jan  1 00:00:00 1970
Fri Jul 27 01:40:53 2012

print $date - $initial;

, $threshold , ,

if ( $date - $lastScanned > $threshold * 24 * 60 * 60 ) { ... }

, , , , . , , . , , , .

+3

All Articles