Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8316983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:29:02+00:00 2026-06-08T21:29:02+00:00

I am working on a Perl script that is to help with the automation

  • 0

I am working on a Perl script that is to help with the automation of scanning of machines on our network. I am not a programmer by trade, but none-the-less this project has been assigned to me, and I am quite stumped. Before I explain the nature of what is stumping me, let me explain the outline of what I am doing.

Basically, this script will be run every n hours. When run, it will check a file that holds a log of active IPs and check them against a DHCP log to single out only those that are static. These then are put into a hash (a new one if flagged to initialize, loaded using Storable otherwise), with the key being the IP and within an array their MAC [0] and a “last scanned” date [1] initially set to 19700101. The next part of the script compares the date between today’s date and the “last scanned” date – and if it under a certain threshold, it sends a query to our scanner.

The issue that has me so lost is that when the date is being checked, it seems to me that the date value (the updated “last scanned”) is being set before entering the conditional. While this doesn’t seem likely to me, it is the only possibility I can think of. Here is the relevant chunks of code:

The code that adds IP/MACs 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.
 }

The code that checks the date (which is set previously, and would be 20120726 for today). Between the above code and the following there is nothing but comments

    $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";

Now, the reason that I believe that the value is being changed before entering the loop is when I add a ‘print $lastScanned’ statement right before the ‘if(($date…){‘ the date printed the whatever is assigned to $date earlier – but if I to comment out the ‘$SCAN{$key}[1] = $date;’ statement, the print statements will print the ‘19700101’ date and everything functions as it should. What is happening? $SCAN{$key}[1] is never being touched except in the two places shown above.

Sorry if this is very badly phrased, or doesn’t make sense. I tried my best to explain something that has been stumping me for hours.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:29:03+00:00Added an answer on June 8, 2026 at 9:29 pm

    Because your @data array is global, every time your execute the statement

    $SCAN{$key} = \@data;
    

    you’re assigning to $SCAN{$key} a reference to the same @data array. Thus, all the values in %SCAN end up pointing to the same array, which is presumably not what you want.

    There are several ways in which you could fix that. Perhaps the simplest would be to make the code assign a reference to a copy of the @data array to $SCAN{$key}, by changing the above line to

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

    Alternatively, you could rewrite the entire loop to use a lexical array declared with my inside the loop — that way you create a new separate array on each iteration:

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

    However, what you really should do, instead of just fixing the symptoms of this particular bug, is learn how variable scoping works in Perl and how it should be used, and rewrite your code accordingly.

    In particular, looking at your code, I very much suspect that you’re not using the strict pragma in your code. If you want to write clean Perl code, the first thing you really should do is prepend the following two lines to all your scripts, immediately after the #! line:

    use strict;
    use warnings;
    

    The strict pragma forces you to avoid certain bad and error-prone habits, such as using symbolic references or undeclared global variables, while the warnings pragma make the interpreter warn you about various other silly, risky, ambiguous or otherwise undesirable things (which you really should treat as errors and fix until you get no more warnings).

    Of course, this doesn’t mean you should just declare all your variables at the beginning of the script with my (or our) just to make strict happy. Instead, what you should do is look at each variable, see where it’s actually used, and declare it in the innermost scope it’s needed in. (If you’re reusing the same variable name in different parts of the code, treat those as separate variables and declare each of them separately.) Remember that you can declare loop variables in the loop statement, as in

    foreach my $key (keys %IPS) {
    

    or

    while (my $line = <>) {
    

    Ps. I also noticed a worrying comment in the code you showed us:

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

    Generally, that kind of code duplication should be a big flashing signal that you’re probably doing something wrong — the golden rule of programming is “Don’t repeat yourself.“

    Of course, there are those few, very very rare occasions where you do need to do essentially the same thing in two different ways, but with so many small and arbitrary differences peppered throughout that it’s cleaner to write the whole thing twice. But I’d be very surprised if that was the case here — I bet you could write that code only once, and just maybe insert an

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

    check at a suitable location.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a perforce-perl script that creates labels. Due to repeated execution
Perl newbie here...I had help with this working perl script with some HASH code
I have a working perl script that grabs the data I need and displays
I've just started working with mongodb got a perl script that parses a twitter
I have some regexes in a Perl script that are correct but slow. I
Sorry, if this to verbose, but I have a perl script that is partly
I'm trying to write a perl script that removes whitespace from XML tags, but
I want to write a perl script that changes its working directory to somewhere
I am working on Perl script that uses Expect to login via telnet to
I'm working on a perl script that parses reddit's JSON using the JSON module.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.