Bob Jane
Bob Jane

Reputation: 57

Useless use of private variable in void context Perl

I am getting the error

Useless use of private variable in void context 

and it is at the line that contains a } only. However, I'm assuming it refers to the line before, which is $final_value -= 4;. I enabled diagnostics to get more info and it returned saying You did something without a side effect in a context that does nothing with the return value But, if I remove the line $final_value -= 4;, the error stops coming, but my program doesn't work anymore. Can I just ignore this and continue ?

Thanks.

Edit- here's a part of the code

elsif($size_of_array == 2){
        if($array[0] > $array[1] && $array[0] >= 4){
            my $final_value = $array[0];
            for($final_value; $final_value > 0;){
                $line =~ s/(.*?)$/"$1\n" . ( q{ } x $final_value . "}")/e;
                $final_value -= 4;          
            }
        }
    }

Upvotes: 0

Views: 972

Answers (1)

Sobrique
Sobrique

Reputation: 53498

The syntax is wrong on your 'for' loop.

Perl expects C style, which means the first 'statement' should initialize, the second test, and the third increment.

Your 'initialize' isn't doing anything: you're just using the variable in a void context - which is what it's warning you about:

#!/usr/bin/env perl

use strict;
use warnings;

my $final_value = 10;

for ( $final_value; $final_value > 0; ) {
   $final_value -= 1;
}

If you initialize $final_value or just leave it blank, it doesn't give you the same error.

for ( ; $final_value > 0; ) {

At this point, you might as well use a while loop.

Alternatively, you can go in the other direction:

for ( my $final_value = 10; $final_value > 0; $final_value -= 1) {
    print $final_value - 1;
}

or

for ( my $final_value = 10; $final_value--; ) {
    print $final_value;
}

Upvotes: 8

Related Questions