Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Useless use of private variable in void context

Tags:

perl

I looked at How can I resolve this case of "Useless use of a variable in a void context"? and it says to use . to concatenate which is what I did but I still the warning.

Useless use of private variable in void context at /mysz/bin/heer line 79.

 43 sub getLog {
 44     opendir(my $dh, $_[0]) || die "can't opendir $_[0]: $!";
 45     my @ooolog = grep {(/^\.oooo_log/)} readdir($dh);
 46     closedir $dh;
 47     return $ooolog[-1];
 48 }

 ...
 79             $ooolog ? ($ooo = $log. "/". &getLog($log)) : $ooo;  <---------
 ...

Not quite sure how to fix Useless use of private variable in void context

Anyone know how to fix it?


EDIT:

78             $ooodata ? ($fl = $fl. "/.ooo_data") : ($fl = $fl. "/.ooo");

if its an ? : issue then why is it not raising the warning at 78?

like image 208
ealeon Avatar asked Dec 15 '22 04:12

ealeon


2 Answers

Consider what happens when $ooolog is false. You end up executing

$ooo;  # Useless

I think you were going for

$ooo = $ooolog ? $log . "/" . &getLog($log) : $ooo;

But it's simpler to do

$ooo = $log . "/" . &getLog($log) if $ooolog;

In the added question, you no longer get the warning because

$ooo;                  # Useless

has been replaced with

$fl = $fl. "/.ooo";    # Not useless

That said,

$ooodata ? ($fl = $fl. "/.ooo_data") : ($fl = $fl. "/.ooo");

is much better written as

$fl .= $ooodata ? "/.ooo_data" : "/.ooo";

If you have an assignment inside of a conditional operator, you are doing something wrong (something suboptimal and/or hard to read).

like image 54
ikegami Avatar answered Jan 15 '23 19:01

ikegami


You've put an expression on line 79, but you don't actually do anything with the result. Thus, it is useless.

A better (and non-useless) way to write your apparent intent would be:

$ooo = $log. "/". &getLog($log) if $ooolog;
like image 21
Dave Sherohman Avatar answered Jan 15 '23 19:01

Dave Sherohman