Perl: How to avoid warnings for superfluous values returned?

Hi all,

a question for the Perl knowledgeable:

I have use warnings; enabled.
I use something like:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

In the further code I only work with some of the returned variables, as the others I have no need for.
Though the turned on warnings complain about this single usage:

Name "main::wday" used only once: possible typo at ./somescript.pl line 39.

I do not want to turn off warnings, neither do I have any usage for those variables/values. If I imagine I had use strict; turned on, I would even get a compilation error...

I had read many answers about people having this problem, but the most common answer was, that you "have to" use the variables.
I mean, I do not know how to be more explicit with localtime() to avoid returned values I do not need, besides writing it to an array and just use the elements I need, which in my eyes would be a stupid workaround (if it works) and not better than the way the warnings complain about.

Enlighten me please and thanks in forward :slight_smile:

Update:
I use now @thetime = localtime(time) and it stopped complaining, when I address the elements I need. Still there is memory used up for the returned values of the function.
The code is worse readable than the 1st example where each variable is assigned.
I do not understand why people explain on their websites, it is a kind of bad habit... Looks more like the limited capability of the warnings to distinguish between a somewhere in the code forgotten variable and a returned value of a function that simply gives back a bunch of values.

You could put that piece of code inside its own block with no warnings enabled.

use warnings;
...
...
{
  no warnings;
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
}
...
...

Alternatively, use the "our" keyword.

use warnings;
...
...
our ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
...
...
1 Like