|
A lot of people consider the errors and warnings of Perl to be great. Usually that's true but in my experience, they can be very confusing to people who are not yet familiar with Perl. I'll describe some of them.
Quick explanationUse of uninitialized value $x in say at perl_warning_1.pl line 6. This means the variable $x has no value (its value is undef). Either it never got a value or something assigned an undef value to it. You should look for the places where the variable got the last assignment or you should try to understand why that piece of code has never been executed.
Basic exampleThe following example will generate such warning. use warnings; use strict; use 5.010; my $x; say $x; It is very nice of Perl that it tells us which file generated the warning and in which line.
Only a warningAs I mentioned this is only a warning, so if the script has more statements after that say statement then they will be executed: use warnings; use strict; use 5.010; my $x; say $x; $x = 42; say $x; This will print Use of uninitialized value $x in say at perl_warning_1.pl line 6. 42
Confusing output orderBeware though, if your code has print statements before the line generating the warning, like in this example: use warnings; use strict; use 5.010; print 'OK'; my $x; say $x; $x = 42; say $x; the result might be confusing. Use of uninitialized value $x in say at perl_warning_1.pl line 7. OK 42 Here the result of the first say is seen after the warning even though it was called before the code that generated the warning. This strangeness is the result of IO buffering. By default Perl buffers STDOUT, the standard output channel, while it does not buffer STDERR, the standard error channel. So while the word 'OK' is waiting for the buffer to be flushed, the warning message already arrives to the screen.
Turning off bufferingIn order to avoid this one can turn off the buffering of STDOUT. This is done by the following code: $| = 1; at the beginning of the script. use warnings; use strict; use 5.010; $| = 1; print 'OK'; my $x; say $x; $x = 42; say $x; OKUse of uninitialized value $x in say at perl_warning_1.pl line 7. 42 (The warning is on the same line as the OK because we have not printed a \n after the OK.)
The unwanted scope
use warnings;
use strict;
use 5.010;
my $x;
my $y = 1;
if ($y) {
my $x = 42;
}
say $x;
This code produces Use of uninitialized value $x in say at perl_warning_1.pl line 11. I have managed to make this mistake several times. By mistake I used my $x inside the if block which meant I have created another $x variable, assigned 42 to it just to let it go out of the scope at the end of the block. (The $y = 1 is just a placeholder for some real code and some real condition.) There are of course cases when I need to declare a variable inside an if block, but not always. When I do that by mistake it is painful to find the bug.
Perl tutorial and video courseFor further articles see the Beginner Perl Maven tutorial book and video course.In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation. blog comments powered by Disqus |
Follow me: