Turn off Perl 5.12 deprecation warnings, if you dare!

Perl 5.12 deprecates several features, for various reasons. Some of the features were always stupid, some need to make way for future development, and some are just too ornery to maintain. All of these are listed in the perldelta5120 documentation. The new thing, however, is that Perl 5.12 will warn you about these even if you don’t have warnings turned on. Consider this script full of Perl whoppers:

use 5.012;

use Switch;             # use given-when
use UNIVERSAL qw(can);  # no more UNIVERSAL->import

$[ = 1;       # I like FORTRAN.
my $pi := 4;  # empty attribute, not a special assignment

OUTER: {
	goto INNER;  # can't jump into inner scope
	...;
	MIDDLE: {
		...;
		INNER: {
			hello();
			}
		}
	}

sub hello :locked { # no more locked attribute
	say 'Here I am!';
	}

When you run this program, you get some warnings:

% perl5.12.1 old_stuff.pl
Use of assignment to $[ is deprecated at old_stuff.pl line 6.
Use of := for an empty attribute list is deprecated at old_stuff.pl line 7.
Use of :locked is deprecated at old_stuff.pl line 20.
Use of "goto" to jump into a construct is deprecated at old_stuff.pl line 9.
Here I am!

If you enable warnings, you get even more deprecation warnings:

% perl5.12.1 -w old_stuff.pl
Switch will be removed from the Perl core distribution in the next major release. Please install it from CPAN. It is being used at old_stuff.pl, line 3.
UNIVERSAL->import is deprecated and will be removed in a future perl at old_stuff.pl line 4
Use of assignment to $[ is deprecated at old_stuff.pl line 6.
Use of := for an empty attribute list is deprecated at old_stuff.pl line 7.
Use of :locked is deprecated at old_stuff.pl line 20.
Use of "goto" to jump into a construct is deprecated at old_stuff.pl line 9.
Here I am!

If you still want to use these features, despite Perl doing everything it can to warn you not to, you can explicitly turn off the deprecation warning class:

use 5.012;
no warnings 'deprecated';

# same as before...

Now you just get the output that you wanted:

$ perl5.12.1 -w old_stuff.pl
Here I am!

You shouldn’t turn off these warnings as a long term strategy. If you’re migrating your ancient Perl to the latest version and want these warnings to temporarily disappear while you focus on some other things, we can let that pass. You might want to give yourself a reminder that you’ve turned off all of these important warnings by checking for warnings as we showed in Item 100: Use lexical warnings to selectively turn on or turn off complaints

use 5.012;
no warnings ‘deprecated’;
temp_warning();

sub temp_warning {
	# needs to be one level lower than the warnings setting
	warn “Hey Evel Knievel! Deprecation warnings are disabled!” unless
		warnings::enabled( ‘deprecated’ );
	}

…;

It's important that you put your check inside a subroutine because warnings::enabled is specifically designed to look on level above where you call it since it's expecting you to use it in a module to respect the state of the calling script.

Since the warnings pragma is lexically scoped, you might have to do this in several places (unless the modules respect the warnings settings of the caller!). Don't expect anyone to rush to make it any easier for you to disengage the safety devices, though!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.