By default new features are not enabled in Perl mainly to retain backward compatibility. Starting with release 5.10, latest features have to be enabled explicitly before using them. Here are a few ways to enable Perl features.
-
Enable new features with the help of
-E
switch.- Introduced in v5.10,
-E
is similar to -e switch except that-E
also enables all latest features. In other words,-E
switch on the Perl command line enables the feature bundle for that version of Perl in the main compilation unit. (see #4 below for enabling feature bundles explicitly).
eg.,
% perl -e 'print $^V;' v5.16.3 % perl -e "print q(Hello)" Hello% ⏎ % perl -E "print q(Hello)" Hello% ⏎ % perl -e "say q(Hello)" syntax error at -e line 1, near "say q(Hello)" Execution of -e aborted due to compilation errors. % perl -E "say q(Hello)" Hello %
- Introduced in v5.10,
-
Enable one or more features selectively with the help of
use feature
pragma.use feature
pragma minimizes the risk of breaking existing programs as the specified feature will only be enabled within the scope of the pragma.
eg.,
% cat -n enable_say.pl 1 #!/bin/perl 2 say("Hello") % ./enable_say.pl Undefined subroutine &main::say called at ./enable_say.pl line 2. % cat -n enable_say.pl 1 #!/bin/perl 2 use feature say; 3 say("Hello") % ./enable_say.pl Hello
To enable multiple features, spearate each one with spaces. eg.,
use feature qw(say state switch unicode_strings)
. -
Enable all features available in the requested version with the help of
use <VERSION>
directive.use <VERSION>
disables any features not in the requested version's feature bundle. (see #4 below for enabling feature bundles explicitly). This is useful to check the current Perl version before using library modules that won't work with older versions of Perl.
eg.,
% cat -n enable_say.pl 1 #!/bin/perl 2 say("Hello") % ./enable_say.pl Undefined subroutine &main::say called at ./enable_say.pl line 2. % cat -n enable_say.pl 1 #!/bin/perl 2 use v5.10; 3 say("Hello") % ./enable_say.pl Hello
-
Similar to
use <VERSION>
directive, feature bundles help load multiple features together. Any feature bundle can be enabled with the help ofuse feature ":<FEATURE_BUNDLE>"
directive.The colon that prefixes the feature bundle differentiates the bundle from an actual feature.
eg.,
To enable
say, state, switch, unicode_strings, unicode_eval, evalbytes, current_sub
andfc
features, enable feature bundle 5.16 as shown below.#!/bin/perl .. use feature ":5.16"; ..
Perl features available in various feature bundles are listed here.
Credit: various sources
No comments:
Post a Comment