Can I use warnings in only one routine?

Im working on a Perl CGI script about 4000 lines. Our coding style includes use strict and use warnings normally, but in this particular (fairly old) file, the “use of warnings” is commented out, with a comment that enabling warnings will flood the Apache log.

Now I plan to separate some code from the new routine. I want to use warnings , at least there. How can I safely limit the use warnings effect to a single routine? Will only the use clause inside the subroutine complete the task?

+4
source share
6 answers

Yes, using use warning will be in the area in which you wrote it.

The use warning entry inside the subtitles will only affect the substitution procedure (or block).


fragment example

 sub foo { use warnings; print my $a; } { use warnings; print my $b; } foo; print my $c; 

Exit

 Use of uninitialized value $b in print at foo.pl line 8. Use of uninitialized value $a in print at foo.pl line 3. 

Note that you are not warned about using print my $c .


What does the documentation say?

  • perldoc.perllexwarn

    This pragma works just like a strict pragma. This means that the scope of the warning pragma is limited to the closing unit. It also means that pragma setup will not flow through files (through use, require or do). This allows authors to independently determine the degree of warnings that will be applied to their module.

+13
source

Yes, as other people have pointed out. But, in my opinion, you better advise to include warnings on a global scale and just disconnect them from problematic sections of code.

 use warnings; sub new_method { # shiny new code } sub old_method { no warnings; # nasty old code } 
+4
source

Yes. From perldoc :

Warnings pragma is a replacement for the -w command line flag, but pragma is limited to the closing block, and the flag is global.

http://perldoc.perl.org/warnings.html

+3
source

Just found the answer myself in "perldoc perllexwarn":

 ...the scope of the warning pragma is limited to the enclosing block. 

So the following should work:

 sub new_method { use warnings; ... } sub old_method { ... # no warnings here } 
+2
source

Turning on alerts around the world and then turning off for certain sections sounds like a good plan.

You do not need to use warnings; or no warnings; inside each sub . You can create special areas with curly braces, for example.

 use warnings; sub nice_new_sub_1 { ... } sub nice_new_sub_2 { ... } { no warnings; sub nasty_old_sub_3 { ... } sub nasty_old_sub_4 { ... } sub nasty_old_sub_5 { ... } } 

Also, consider disabling only those alerts that need to be completed in order for them to work correctly, for example

 { no strict 'refs'; sub nasty_old_sub_3 { ... } sub nasty_old_sub_4 { ... } } 
+2
source

The answer to use warnings can be used lexically correctly. The decision to use no warnings lexical and use warnings globally is more correct. The best decision is to fix all your warnings. Somewhere between them there is a redirection of errors.

 use warnings; open STDERR, ">>", "foo/error.log" or die $!; 

Leave this for a while, then run:

 perl -nlwe '$a{$_}++ }{ print for keys %a' foo/error.log > foo/errors.dedupe 

Go through the code and correct the warnings. Most likely, if the script works, they will be trivial. But if you don’t check, how do you know?

If at the end you decide that it is not a hassle to fix all warnings, simply remove the warnings and redirect errors and use the warnings lexically.

+2
source

Source: https://habr.com/ru/post/1401330/


All Articles