Thursday, April 28, 2022

[FIXED] What is the "once" warnings in perl?

Issue

I have code that has,

no warnings 'once';

Reading man warnings I don't see an occurrence of /once/ what does this do?


Solution

So long as you don't have strict on, perl allows you to use a variable without declaring it.

perl -wE'$foo = 4;'

Which outputs,

Name main::foo used only once: possible typo at -e line 1.

Note under strict this wouldn't even be permitted,

Global symbol $foo requires explicit package name (did you forget to declare my $foo?) at -e line 1.

You can disable the warning though, without enabling strict by doing no warnings "once"; Though I would suggest strongly you simply remove the unused code instead of silencing the warning.

perl -wE'no warnings "once"; $foo = 4;'

Which both looks ugly and does nothing.



Answered By - Evan Carroll
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.