perl

how to solve Useless use of private variable in void context, Use of uninitialized value $interval in division (/) and Illegal division by zero at


I don't have any experience in working with Perl, and so I m not able to solve this issues. Can you please help me out on solving this :

Error 1 : Useless use of private variable in void context at ./createnodelist.pl line 51.
Error 2 : Use of uninitialized value $interval in division (/) at ./createnodelist.pl line 10.
Error 3 : Illegal division by zero at ./createnodelist.pl line 10.

Basically the below script should load the list of certificates and produces a list of nodes which minutes assigned to a node :

line 51    print FILE "\n";

line 10   my $jph = ceil(60/$interval);

Source code:

#!/usr/bin/perl

use warnings;
use strict;

use POSIX;

my $interval = $ARGV[0];
my $path     = '/etc/puppet/modules/puppet/scripts/puppet_cron';

# Calculate how many jobs per hour we have
my $jph = ceil( 60 / $interval );

# Load list of all nodes
my @nodes = `/usr/sbin / puppetca -la | /usr/ bin / cut -d ' ' -f 2`;    #
chomp(@nodes);

# Count number of nodes
my $node_count = scalar(@nodes);

# Count number of nodes per group
my $nodes_per_group = $node_count / $interval;

# Process nodes list and assigne minutes for each node
open( FILE, ">$path/nodes.list" );
for ( my $i = 0; $i < $interval; $i++ ) {
    my $minute;
    my $group = $i + 1;
    my $node_n;

    # Process nodes in group
    for ( $node_n = 0; $node_n < $nodes_per_group; $node_n ) {

        # Assign minutes to a node
        my $node = shift(@nodes);
        if ($node) {
            print FILE "$node;";
            for ( my $n = 0; $n < $jph; $n++ ) {
                $minute = $i + ( $interval * $n );
                if ( $minute
                    < 60 )    # Only print minutes that actually exist
                {
                    print FILE "$minute";
                }
                if ( $n != $jph - 1 ) {
                    print FILE ',';
                }
            }
        }
        $node_n++;
        print FILE "\n";
    }
}
close(FILE);
exit 0;

Solution

  • First rule of solving perl problems:

    Set use strict; use warnings;.

    Second: Read the error messages.

    Your problem is on line 10: Divide by zero. The only possibility is $interval is zero. Or undefined. Or uninitialized. If you turn on strict/warnings, then it may give you a warning about the latter.

    On line 51: Useless use of a private variable in a void context - means basically that statement isn't doing anything. Probably means FILE hasn't been opened properly.

    Edit: With your code posted: $interval is being set to $ARGV[0]. What argument are you supplying on the command line? If no value is supplied, then you'll get your divide by zero error, because $interval is undefined.

    From your comment - you're calling it without an argument. Therefore $interval will always be undefined, and that'll give you the error messages you want. Either:

    • call the script with an argument

    • set a default for $interval e.g. my $interval = $ARGV[0] || 60;

    For your second problem - useless use of a private variable in a void context:

    for ( $node_n = 0; $node_n < $nodes_per_group; $node_n ) {
    

    You're not incrementing $node_n it's being used in a void context. Hence the warning.

    for ( $node_n = 0; $node_n < $nodes_per_group; $node_n++ ) {